在php中spl_autoload_register與__autoload方法是php5才有的,下面我來(lái)給大家介紹這兩個(gè)魔術(shù)函數(shù)的使用方法,大家可進(jìn)入了解了解.
spl_autoload_register()函數(shù)應(yīng)該是主流框架使用最多的也是非常核心的函數(shù)之一,可實(shí)現(xiàn)自動(dòng)注冊(cè)函數(shù)和類(lèi),實(shí)現(xiàn)類(lèi)似__autoload() 函數(shù)功能,簡(jiǎn)化了類(lèi)的調(diào)用與加載,提高了工作的效率。
支持版本:PHP 5 >= 5.1.2
至于效率問(wèn)題,php手冊(cè)上有如此之話(huà):bool spl_autoload_register ([ callback $autoload_function ] )
將函數(shù)注冊(cè)到SPL __autoload函數(shù)棧中。如果該棧中的函數(shù)尚未激活,則激活它們。如果在你的程序中已經(jīng)實(shí)現(xiàn)了__autoload函數(shù),它必須顯式注冊(cè)到__autoload棧中。因?yàn)閟pl_autoload_register()函數(shù)會(huì)將Zend Engine中的__autoload函數(shù)取代為spl_autoload()或spl_autoload_call()。
spl_autoload_register
(PHP 5 >= 5.1.2)
spl_autoload_register — 注冊(cè)__autoload()函數(shù)
說(shuō)明:bool spl_autoload_register ([ callback $autoload_function ] )
將函數(shù)注冊(cè)到SPL __autoload函數(shù)棧中,如果該棧中的函數(shù)尚未激活,則激活它們,如果在你的程序中已經(jīng)實(shí)現(xiàn)了__autoload函數(shù),它必須顯式注冊(cè)到__autoload棧中,因?yàn)閟pl_autoload_register()函數(shù)會(huì)將Zend Engine中的__autoload函數(shù)取代為spl_autoload()或spl_autoload_call().
參數(shù):autoload_function 欲注冊(cè)的自動(dòng)裝載函數(shù),如果沒(méi)有提供任何參數(shù),則自動(dòng)注冊(cè)autoload的默認(rèn)實(shí)現(xiàn)函數(shù)
spl_autoload()。
返回值:如果成功則返回 TRUE,失敗則返回 FALSE.
注:SPL是Standard PHP Library(標(biāo)準(zhǔn)PHP庫(kù))的縮寫(xiě),它是PHP5引入的一個(gè)擴(kuò)展庫(kù),其主要功能包括autoload機(jī)制的實(shí)現(xiàn)及包括各種Iterator接口或類(lèi),SPL autoload機(jī)制的實(shí)現(xiàn)是通過(guò)將函數(shù)指針autoload_func指向自己實(shí)現(xiàn)的具有自動(dòng)裝載功能的函數(shù)來(lái)實(shí)現(xiàn)的,SPL有兩個(gè)不同的函數(shù) spl_autoload,spl_autoload_call,通過(guò)將autoload_func指向這兩個(gè)不同的函數(shù)地址來(lái)實(shí)現(xiàn)不同的自動(dòng)加載機(jī)制,代碼如下:
- <?php
- class autoload
- {
- public static function load( $class name )
- {
- $filename = $classname.".class.php";
- if (file_exists($filename )) {
- require_once $filename ;
- }
- }
- }
- function __autoload( $class name )
- {
- // 這個(gè)是默認(rèn)的 autoload 方法
- $filename = $classname.".class.php";
- if (file_exists($filename )) {
- require_once $filename ;
- } //開(kāi)源代碼phpfensi.com
- }
- // 注冊(cè)一個(gè) autoloader
- spl_autoload_register( 'autoload::load' );
- spl_autoload_register( '__autoload' );
- // 注:下面的類(lèi)看上去沒(méi)有定義,但其實(shí)系統(tǒng)根據(jù)sql_autoload_register提供的路徑會(huì)自動(dòng)去搜索
- //foo.class.php文件,如果沒(méi)找到才報(bào)錯(cuò)。
- $foo = new foo();
- $foo ->bar();
- ?>
在補(bǔ)充下:__autoload 方法在 spl_autoload_register 后會(huì)失效,因?yàn)?autoload_func 函數(shù)指針已指向 spl_autoload 方法.
可以通過(guò)下面的方法來(lái)把 _autoload 方法加入 autoload_functions list
spl_autoload_register( '__autoload' );
此外我們還可以使用我們自定義的加載方法.
第一種函數(shù)式,代碼如下:
- function my_own_loader($classname)
- {
- $class_file = strtolower($classname).".php";
- if (file_exists($class_file)){
- require_once($class_file);
- }
- }
- spl_autoload_register("my_own_loader");
- $a = new A();
第二種類(lèi)式,代碼如下:
- class Loader
- {
- public static function my_own_loader($classname)
- {
- $class_file = strtolower($classname).".php";
- if (file_exists($class_file)){
- require_once($class_file);
- }
- }
- }
- // 通過(guò)數(shù)組的形式傳遞類(lèi)和方法的名稱(chēng)
- spl_autoload_register(array("Loader","my_own_loader"));
- $a = new A();
實(shí)例:CI框架實(shí)現(xiàn)類(lèi)加載的同時(shí),其對(duì)應(yīng)的model也生成,代碼如下:
- static public function myAutoload($class){
- if(file_exists(APPPATH.'models'.DIRECATORY_SEPARATOR.$class.'.php')){
- require_once APPPATH.'models'.DIRECATORY_SEPARATOR.$class.'.php';
- }
- }
- /**
- * 注冊(cè)加載器
- */
- static public function autoload(){
- spl_autoload_register(array(__CLASS__, 'myAutoload'));
- if(class_exists('__autoload')){
- spl_autoload_register('__autoload');
- }
- }
- MY_Controller::autoload();
當(dāng)然上面只是最簡(jiǎn)單的示范,__autoload只是去include_path尋找類(lèi)文件并加載,我們可以根據(jù)自己的需要定義__autoload加載類(lèi)的規(guī)則.
此外,假如我們不想自動(dòng)加載的時(shí)候調(diào)用__autoload,而是調(diào)用我們自己的函數(shù)(或者類(lèi)方法),我們可以使用spl_autoload_register來(lái)注冊(cè)我們自己的autoload函數(shù),它的函數(shù)原型如下:
bool spl_autoload_register ( [callback $autoload_function] )
我們繼續(xù)改寫(xiě)上面那個(gè)例子,代碼如下:
- function loader($class)
- {
- $file = $class . '.php';
- if (is_file($file)) {
- require_once($file);
- }
- }
- spl_autoload_register('loader');
- $a = new A();
- function loader($class)
- {
- $file = $class . '.php';
- if (is_file($file)) {
- require_once($file);
- }
- }
- spl_autoload_register('loader');
- $a = new A();
這樣子也是可以正常運(yùn)行的,這時(shí)候php在尋找類(lèi)的時(shí)候就沒(méi)有調(diào)用__autoload而是調(diào)用我們自己定義的函數(shù)loader了,同樣的道理,下面這種寫(xiě)法也是可以的,代碼如下:
轉(zhuǎn)載請(qǐng)注明來(lái)源:php中SPL spl_autoload_register與__autoload方法使用
- <?php
- class Loader
- {
- public static function loadClass($class)
- {
- $file = $class . '.php';
- if (is_file($file)) {
- require_once($file);
- }
- }
- }
- spl_autoload_register(array('Loader', 'loadClass'));
- $a = new A();
- ?>
哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀(guān)的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無(wú)需額外費(fèi)用,即可穩(wěn)步提升排名至首頁(yè)。歡迎體驗(yàn)最佳的哈爾濱網(wǎng)站建設(shè)。
