首頁  >  文章  >  後端開發  >  php關於use、命名空間、引入類別檔案和自動載入類別的實例詳解

php關於use、命名空間、引入類別檔案和自動載入類別的實例詳解

伊谢尔伦
伊谢尔伦原創
2017-07-01 10:07:476137瀏覽

use只是使用了命名空間
但是要想呼叫類,必須載入類文件,或自動載入

php關於use、命名空間、引入類別檔案和自動載入類別的實例詳解

php關於use、命名空間、引入類別檔案和自動載入類別的實例詳解

#即便引入了其中一個類,如果沒有自動載入機制,還是會報錯

php關於use、命名空間、引入類別檔案和自動載入類別的實例詳解

php關於use、命名空間、引入類別檔案和自動載入類別的實例詳解

use的幾種用法

namespace Blog\Article;
class Comment { }

//创建一个BBS空间(我有打算开个论坛)
namespace BBS;

//导入一个命名空间
use Blog\Article;
//导入命名空间后可使用限定名称调用元素
$article_comment = new Article\Comment();

//为命名空间使用别名
use Blog\Article as Arte;
//使用别名代替空间名
$article_comment = new Arte\Comment();

//导入一个类
use Blog\Article\Comment;
//导入类后可使用非限定名称调用元素
$article_comment = new Comment();

//为类使用别名
use Blog\Article\Comment as Comt;
//使用别名代替空间名
$article_comment = new Comt();

php關於use、命名空間、引入類別檔案和自動載入類別的實例詳解

#1.第一種引入方式(前提是有了自動加載機制)

use OSS\OssClient; // 表示引入Class 'OSS\OssClient'

使用的時候,

$ossClient = new OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint, false);

或這樣

$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false);

都可以!

2.第二種引入方式(前提是有了自動載入機制)

import('@.ORG.OSS.OssClient'); // thinkphp中的加载机制

使用的時候,只能

$ossClient = new OSS\OssClient($accessKeyId, $accessKeySecret, $endpoint, false);  // 其中OSS是命名空间

thinkphp中有一個自動載入命名空間的機制,

框架Liberary目錄下的命名空間都可以自動識別和定位,如下

Library 框架類別庫目錄
│ ├─Think 核心Think類別庫包目錄
│ ├─Org Org類別庫包目錄
│ ├─ ... 更多類別庫目錄

所以,如果有命名空間,不需要引入檔案也可以。
但是沒有命名空間的類,如果不引入文件,就會報錯。
php關於use、命名空間、引入類別檔案和自動載入類別的實例詳解

php關於use、命名空間、引入類別檔案和自動載入類別的實例詳解

php關於use、命名空間、引入類別檔案和自動載入類別的實例詳解

import一下就可以了,
php關於use、命名空間、引入類別檔案和自動載入類別的實例詳解

php關於use、命名空間、引入類別檔案和自動載入類別的實例詳解

3.autoload

這是一個自動載入函數,在PHP5中,當我們實例化一個未定義的類別時,就會觸發此函數。看下面範例:

printit.class.php 
 
<?php 
 
class PRINTIT { 
 
 function doPrint() {
  echo &#39;hello world&#39;;
 }
}
?> 
 
index.php 
 
<?
function autoload( $class ) {
 $file = $class . &#39;.class.php&#39;;  
 if ( is_file($file) ) {  
  require_once($file);  
 }
} 
 
$obj = new PRINTIT();
$obj->doPrint();
?>

運行index.php後正常輸出hello world。在index.php中,由於沒有包含printit.class.php,在實例化printit時,自動呼叫autoload函數,參數$class的值即為類別名稱printit,此時printit.class.php就被引進來了。

4.spl_autoload_register
再看spl_autoload_register(),這個函數與autoload有與曲同工之妙,看個簡單的例子:

<?
function loadprint( $class ) {
 $file = $class . &#39;.class.php&#39;;  
 if (is_file($file)) {  
  require_once($file);  
 } 
} 
 
spl_autoload_register( &#39;loadprint&#39; ); 
 
$obj = new PRINTIT();
$obj->doPrint();
?>

將autoload換成loadprint函數。但loadprint不會像autoload自動觸發,這時spl_autoload_register()就運作了,它告訴PHP碰到沒有定義的類別就執行loadprint()。

spl_autoload_register() 呼叫靜態方法 ,

<? 
 
class test {
 public static function loadprint( $class ) {
  $file = $class . &#39;.class.php&#39;;  
  if (is_file($file)) {  
   require_once($file);  
  } 
 }
} 
 
spl_autoload_register(  array(&#39;test&#39;,&#39;loadprint&#39;)  );
//另一种写法:spl_autoload_register(  "test::loadprint"  ); 
 
$obj = new PRINTIT();
$obj->doPrint();
?>

以上是php關於use、命名空間、引入類別檔案和自動載入類別的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn