在php中spl_autoload_register與__autoload方法是php5才有的,下面我來給大家介紹這兩個魔術(shù)函數(shù)的使用方法,大家可進(jìn)入了解了解.

spl_autoload_register()函數(shù)應(yīng)該是主流框架使用最多的也是非常核心的函數(shù)之一,可實現(xiàn)自動注冊函數(shù)和類,實現(xiàn)類似__autoload() 函數(shù)功能,簡化了類的調(diào)用與加載,提高了工作的效率。

支持版本:PHP 5 >= 5.1.2

至于效率問題,php手冊上有如此之話:bool spl_autoload_register ([ callback $autoload_function ] )

將函數(shù)注冊到SPL __autoload函數(shù)棧中。如果該棧中的函數(shù)尚未激活,則激活它們。如果在你的程序中已經(jīng)實現(xiàn)了__autoload函數(shù),它必須顯式注冊到__autoload棧中。因為spl_autoload_register()函數(shù)會將Zend Engine中的__autoload函數(shù)取代為spl_autoload()或spl_autoload_call()。

spl_autoload_register

(PHP 5 >= 5.1.2)

spl_autoload_register — 注冊__autoload()函數(shù)

說明:bool spl_autoload_register ([ callback $autoload_function ] )

將函數(shù)注冊到SPL __autoload函數(shù)棧中,如果該棧中的函數(shù)尚未激活,則激活它們,如果在你的程序中已經(jīng)實現(xiàn)了__autoload函數(shù),它必須顯式注冊到__autoload棧中,因為spl_autoload_register()函數(shù)會將Zend Engine中的__autoload函數(shù)取代為spl_autoload()或spl_autoload_call().

參數(shù):autoload_function 欲注冊的自動裝載函數(shù),如果沒有提供任何參數(shù),則自動注冊autoload的默認(rèn)實現(xiàn)函數(shù)

spl_autoload()。

返回值:如果成功則返回 TRUE,失敗則返回 FALSE.

注:SPL是Standard PHP Library(標(biāo)準(zhǔn)PHP庫)的縮寫,它是PHP5引入的一個擴(kuò)展庫,其主要功能包括autoload機(jī)制的實現(xiàn)及包括各種Iterator接口或類,SPL autoload機(jī)制的實現(xiàn)是通過將函數(shù)指針autoload_func指向自己實現(xiàn)的具有自動裝載功能的函數(shù)來實現(xiàn)的,SPL有兩個不同的函數(shù) spl_autoload,spl_autoload_call,通過將autoload_func指向這兩個不同的函數(shù)地址來實現(xiàn)不同的自動加載機(jī)制,代碼如下:

  1. <?php  
  2. class autoload  
  3. {  
  4.   public static function load( $class name )  
  5.   {  
  6.     $filename = $classname.".class.php";  
  7.     if (file_exists($filename )) {  
  8.       require_once $filename ;  
  9.     }  
  10.   }  
  11. }  
  12.    
  13. function __autoload( $class name )  
  14. {  
  15.     // 這個是默認(rèn)的 autoload 方法  
  16.     $filename = $classname.".class.php";  
  17.     if (file_exists($filename )) {  
  18.       require_once $filename ;  
  19.     } //開源代碼phpfensi.com 
  20. // 注冊一個 autoloader  
  21. spl_autoload_register( 'autoload::load' );  
  22. spl_autoload_register( '__autoload' );  
  23.  // 注:下面的類看上去沒有定義,但其實系統(tǒng)根據(jù)sql_autoload_register提供的路徑會自動去搜索  
  24. //foo.class.php文件,如果沒找到才報錯。  
  25. $foo = new foo();  
  26. $foo ->bar();  
  27. ?> 

在補(bǔ)充下:__autoload 方法在 spl_autoload_register 后會失效,因為 autoload_func 函數(shù)指針已指向 spl_autoload 方法.

可以通過下面的方法來把 _autoload 方法加入 autoload_functions list

spl_autoload_register( '__autoload' );

此外我們還可以使用我們自定義的加載方法.

第一種函數(shù)式,代碼如下:

  1. function my_own_loader($classname)  
  2. {  
  3.     $class_file = strtolower($classname).".php";  
  4.     if (file_exists($class_file)){  
  5.         require_once($class_file);  
  6.     }  
  7. }  
  8.    
  9. spl_autoload_register("my_own_loader");  
  10.    
  11. $a = new A(); 

第二種類式,代碼如下:

  1. class Loader  
  2. {  
  3.     public static function my_own_loader($classname)  
  4.     {  
  5.         $class_file = strtolower($classname).".php";  
  6.         if (file_exists($class_file)){  
  7.             require_once($class_file);  
  8.         }  
  9.     }  
  10. }  
  11.    
  12. // 通過數(shù)組的形式傳遞類和方法的名稱  
  13. spl_autoload_register(array("Loader","my_own_loader"));  
  14. $a = new A(); 

實例:CI框架實現(xiàn)類加載的同時,其對應(yīng)的model也生成,代碼如下:

  1. static public function myAutoload($class){  
  2.   if(file_exists(APPPATH.'models'.DIRECATORY_SEPARATOR.$class.'.php')){  
  3.      require_once APPPATH.'models'.DIRECATORY_SEPARATOR.$class.'.php';  
  4.   }  
  5. }  
  6. /**  
  7. * 注冊加載器  
  8. */ 
  9. static public function autoload(){  
  10.    spl_autoload_register(array(__CLASS__'myAutoload'));  
  11.    if(class_exists('__autoload')){  
  12.       spl_autoload_register('__autoload');  
  13.    }  
  14. }  
  15. MY_Controller::autoload(); 

當(dāng)然上面只是最簡單的示范,__autoload只是去include_path尋找類文件并加載,我們可以根據(jù)自己的需要定義__autoload加載類的規(guī)則.

此外,假如我們不想自動加載的時候調(diào)用__autoload,而是調(diào)用我們自己的函數(shù)(或者類方法),我們可以使用spl_autoload_register來注冊我們自己的autoload函數(shù),它的函數(shù)原型如下:

bool spl_autoload_register ( [callback $autoload_function] )

我們繼續(xù)改寫上面那個例子,代碼如下:

  1. function loader($class)    
  2. {    
  3. $file = $class . '.php';    
  4. if (is_file($file)) {    
  5. require_once($file);    
  6. }    
  7. }   
  8. spl_autoload_register('loader');   
  9. $a = new A(); 
  10.  
  11. function loader($class
  12. $file = $class . '.php'
  13. if (is_file($file)) { 
  14. require_once($file); 
  15. spl_autoload_register('loader'); 
  16. $a = new A(); 

這樣子也是可以正常運(yùn)行的,這時候php在尋找類的時候就沒有調(diào)用__autoload而是調(diào)用我們自己定義的函數(shù)loader了,同樣的道理,下面這種寫法也是可以的,代碼如下:

  1. <?php    
  2. class Loader    
  3. {    
  4. public static function loadClass($class)    
  5. {    
  6. $file = $class . '.php';    
  7. if (is_file($file)) {    
  8. require_once($file);    
  9. }    
  10. }    
  11. }   
  12. spl_autoload_register(array('Loader''loadClass'));   
  13. $a = new A(); 
  14. ?>
轉(zhuǎn)載請注明來源:php中SPL spl_autoload_register與__autoload方法使用

  哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無需額外費(fèi)用,即可穩(wěn)步提升排名至首頁。歡迎體驗最佳的哈爾濱網(wǎng)站建設(shè)。