在php中spl_autoload_register與__autoload方法是php5才有的,下面我來給大家介紹這兩個魔術函數的使用方法,大家可進入了解了解.
spl_autoload_register()函數應該是主流框架使用最多的也是非常核心的函數之一,可實現自動注冊函數和類,實現類似__autoload() 函數功能,簡化了類的調用與加載,提高了工作的效率。
支持版本:PHP 5 >= 5.1.2
至于效率問題,php手冊上有如此之話:bool spl_autoload_register ([ callback $autoload_function ] )
將函數注冊到SPL __autoload函數棧中。如果該棧中的函數尚未激活,則激活它們。如果在你的程序中已經實現了__autoload函數,它必須顯式注冊到__autoload棧中。因為spl_autoload_register()函數會將Zend Engine中的__autoload函數取代為spl_autoload()或spl_autoload_call()。
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指向這兩個不同的函數地址來實現不同的自動加載機制,代碼如下:
- <?php
- class autoload
- {
- public static function load( $class name )
- {
- $filename = $classname.".class.php";
- if (file_exists($filename )) {
- require_once $filename ;
- }
- }
- }
- function __autoload( $class name )
- {
- // 這個是默認的 autoload 方法
- $filename = $classname.".class.php";
- if (file_exists($filename )) {
- require_once $filename ;
- } //開源代碼Vevb.com
- }
- // 注冊一個 autoloader
- spl_autoload_register( 'autoload::load' );
- spl_autoload_register( '__autoload' );
- // 注:下面的類看上去沒有定義,但其實系統根據sql_autoload_register提供的路徑會自動去搜索
- //foo.class.php文件,如果沒找到才報錯。
- $foo = new foo();
- $foo ->bar();
- ?>
在補充下:__autoload 方法在 spl_autoload_register 后會失效,因為 autoload_func 函數指針已指向 spl_autoload 方法.
可以通過下面的方法來把 _autoload 方法加入 autoload_functions list
spl_autoload_register( '__autoload' );
此外我們還可以使用我們自定義的加載方法.
第一種函數式,代碼如下:
- 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();
第二種類式,代碼如下:
- class Loader
- {
- public static function my_own_loader($classname)
- {
- $class_file = strtolower($classname).".php";
- if (file_exists($class_file)){
- require_once($class_file);
- }
- }
- }
- // 通過數組的形式傳遞類和方法的名稱
- spl_autoload_register(array("Loader","my_own_loader"));
- $a = new A();
實例:CI框架實現類加載的同時,其對應的model也生成,代碼如下:
- static public function myAutoload($class){
- if(file_exists(APPPATH.'models'.DIRECATORY_SEPARATOR.$class.'.php')){
- require_once APPPATH.'models'.DIRECATORY_SEPARATOR.$class.'.php';
- }
- }
- /**
- * 注冊加載器
- */
- static public function autoload(){
- spl_autoload_register(array(__CLASS__, 'myAutoload'));
- if(class_exists('__autoload')){
- spl_autoload_register('__autoload');
- }
- }
- MY_Controller::autoload();
當然上面只是最簡單的示范,__autoload只是去include_path尋找類文件并加載,我們可以根據自己的需要定義__autoload加載類的規則.
此外,假如我們不想自動加載的時候調用__autoload,而是調用我們自己的函數(或者類方法),我們可以使用spl_autoload_register來注冊我們自己的autoload函數,它的函數原型如下:
bool spl_autoload_register ( [callback $autoload_function] )
我們繼續改寫上面那個例子,代碼如下:
- 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();
這樣子也是可以正常運行的,這時候php在尋找類的時候就沒有調用__autoload而是調用我們自己定義的函數loader了,同樣的道理,下面這種寫法也是可以的,代碼如下:
- <?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();
- ?>
新聞熱點
疑難解答