關於PHP中spl_autoload_register()函數用法詳解

來源:互聯網
上載者:User
這篇文章主要介紹了PHP中spl_autoload_register()函數用法,結合執行個體形式分析了__autoload函數及spl_autoload_register函數的相關提示,需要的朋友可以參考下

在瞭解這個函數之前先來看另一個函數:__autoload。

一、__autoload

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

printit.class.php:

<?phpclass PRINTIT { function doPrint() { echo 'hello world'; }}?>

index.php

<?function __autoload( $class ) { $file = $class . '.class.php'; 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就被引進來了。

在物件導向中這種方法經常使用,可以避免書寫過多的引用檔案,同時也使整個系統更加靈活。

二、spl_autoload_register()

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

<?function loadprint( $class ) { $file = $class . '.class.php'; if (is_file($file)) { require_once($file); }}spl_autoload_register( 'loadprint' );$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 . '.class.php'; if (is_file($file)) {  require_once($file); } }}spl_autoload_register( array('test','loadprint') );//另一種寫法:spl_autoload_register( "test::loadprint" );$obj = new PRINTIT();$obj->doPrint();?>

spl_autoload_register

(PHP 5 >= 5.1.2)

spl_autoload_register — 註冊__autoload()函數

說明

bool spl_autoload_register ([ callback $autoload_function ] )
將函數註冊到SPL __autoload函數棧中。如果該棧中的函數尚未啟用,則啟用它們。

如果在你的程式中已經實現了__autoload函數,它必須顯式註冊到__autoload棧中。因為spl_autoload_register()函數會將Zend Engine中的__autoload函數取代為spl_autoload() 或 spl_autoload_call()。

參數

autoload_function

欲註冊的自動裝載函數。如果沒有提供任何參數,則自動註冊autoload的預設實現函數spl_autoload()。

傳回值

如果成功則返回 TRUE,失敗則返回 FALSE。

註:SPL是Standard PHP Library(標準PHP庫)的縮寫。它是PHP5引入的一個擴充庫,其主要功能包括autoload機制的實現及包括各種Iterator介面或類。SPL autoload機制的實現是通過將函數指標autoload_func指向自己實現的具有自動裝載功能的函數來實現的。SPL有兩個不同的函數spl_autoload, spl_autoload_call,通過將autoload_func指向這兩個不同的函數地址來實現不同的自動載入機制。

classLOAD{ staticfunctionloadClass($class_name)  {    $filename= $class_name.".class.php"; $path= "include/".$filename    if(is_file($path)) returninclude$path;  }}/** * 設定對象的自動載入 * spl_autoload_register — Register given function as __autoload() implementation */spl_autoload_register(array('LOAD', 'loadClass'));/***__autoload 方法在 spl_autoload_register 後會失效,因為 autoload_func 函數指標已指向 spl_autoload 方法* 可以通過下面的方法來把 _autoload 方法加入 autoload_functions list*/spl_autoload_register( '__autoload');

如果同時用spl_autoload_register註冊了一個類的方法和__autoload函數,那麼,會根據註冊的先後,如果在第一個註冊的方法或函數裡載入了類檔案,就不會再執行第二個被註冊的類的方法或函數。反之就會執行第二個被註冊的類的方法或函數。

<?phpclass autoloader {  public static $loader;  public static function init() {    if (self::$loader == NULL)      self::$loader = new self ();    return self::$loader;  }  public function __construct() {    spl_autoload_register ( array ($this, 'model' ) );    spl_autoload_register ( array ($this, 'helper' ) );    spl_autoload_register ( array ($this, 'controller' ) );    spl_autoload_register ( array ($this, 'library' ) );  }  public function library($class) {    set_include_path ( get_include_path () . PATH_SEPARATOR . '/lib/' );    spl_autoload_extensions ( '.library.php' );    spl_autoload ( $class );  }  public function controller($class) {    $class = preg_replace ( '/_controller$/ui', '', $class );    set_include_path ( get_include_path () . PATH_SEPARATOR . '/controller/' );    spl_autoload_extensions ( '.controller.php' );    spl_autoload ( $class );  }  public function model($class) {    $class = preg_replace ( '/_model$/ui', '', $class );    set_include_path ( get_include_path () . PATH_SEPARATOR . '/model/' );    spl_autoload_extensions ( '.model.php' );    spl_autoload ( $class );  }  public function helper($class) {    $class = preg_replace ( '/_helper$/ui', '', $class );    set_include_path ( get_include_path () . PATH_SEPARATOR . '/helper/' );    spl_autoload_extensions ( '.helper.php' );    spl_autoload ( $class );  }}//callautoloader::init ();?>

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.