關於PHP的autoLoad自動載入機制的分析

來源:互聯網
上載者:User
這篇文章主要介紹了關於PHP的autoLoad自動載入機制的分析,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

php的autoload大致可以使用兩種方法:__autoload和spl方法。這兩種方法又各有不同的幾種使用方法

__autoload的使用方法1:
最經常使用的就是這種方法,根據類名,找出類檔案,然後require_one

function __autoload($class_name) { $path = str_replace('_', '/', $class_name); require_once $path . '.php'; } // 這裡會自動載入Http/File/Interface.php 檔案 $a = new Http_File_Interface();

這種方法的好處就是簡單易使用。當然也有缺點,缺點就是將類名和檔案路徑強製做了約定,當修改檔案結構的時候,就勢必要修改類名。

__autoload的使用方法2(直接映射法)

$map = array( 'Http_File_Interface' => 'C:/PHP/HTTP/FILE/Interface.php' ); function __autoload($class_name) { if (isset($map[$class_name])) { require_once $map[$class_name]; } } // 這裡會自動載入C:/PHP/HTTP/FILE/Interface.php 檔案 $a = new Http_File_Interface();

這種方法的好處就是類名和檔案路徑只是用一個映射來維護,所以當檔案結構改變的時候,不需要修改類名,只需要將映射中對應的項修改就好了。
這種方法相較於前面的方法缺點是當檔案多了的時候這個映射維護起來非常麻煩,或許這時候你就會考慮使用json或者單獨一個檔案來進行維護了。或許你會想到使用一個架構來維護或者建立這麼一個映射。
spl_autoload
__autoload的最大缺陷是無法有多個autoload方法
好了, 想下下面的這個情景,你的項目引用了別人的一個項目,你的項目中有一個__autoload,別人的項目也有一個__autoload,這樣兩個__autoload就衝突了。解決的辦法就是修改__autoload成為一個,這無疑是非常繁瑣的。
因此我們急需使用一個autoload呼叫堆疊,這樣spl的autoload系列函數就出現了。你可以使用spl_autoload_register註冊多個自訂的autoload函數
如果你的PHP版本大於5.1的話,你就可以使用spl_autoload
先瞭解spl的幾個函數:


spl_autoload 是_autoload()的預設實現,它會去include_path中尋找$class_name(.php/.inc)
Spl_autoload實現自動載入:

/*http.php*/ <?php class http { public function callname(){ echo "this is http"; } } /*test.php*/ <?php set_include_path("/home/yejianfeng/handcode/"); //這裡需要將路徑放入include spl_autoload("http"); //尋找/home/yejianfeng/handcode/http.php $a = new http(); $a->callname();

Spl_autoload_register

將函數註冊到SPL __autoload函數棧中,直接看一個例子:

/*http.php*/ <?php class http { public function callname(){ echo "this is http"; } } /*test.php*/ <?php spl_autoload_register(function($class){ if($class == 'http'){ require_once("/home/yejianfeng/handcode/http.php"); } }); $a = new http(); $a->callname(); spl_autoload_call

調用spl_autoload_register中註冊的調用函數, 看下面的例子

/*http.php*/ <?php class http { public function callname(){ echo "this is http"; } } /*http2.php*/ <?php class http { public function callname(){ echo "this is http2"; } } /*test.php*/ <?php spl_autoload_register(function($class){ if($class == 'http'){ require_once("/home/yejianfeng/handcode/http.php"); } if($class == 'http2'){ require_once("/home/yejianfeng/handcode/http2.php"); } }); spl_auto_call('http2'); $a = new http(); $a->callname(); //這個時候會輸出"this is http2"

spl_auto_register這個函數使得我們不使用__autoload,使用自訂的函數來進行自動載入成為可能。這個方法現在是經常使用到的。
Zend的AutoLoader模組就使用了這個方法。摘錄其中對應的代碼

spl_autoload_register(array(__CLASS__, 'autoload')); public static function autoload($class) { ….. }

以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.