首頁  >  文章  >  後端開發  >  關於PHP中spl_autoload_register()函數用法詳解

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

墨辰丷
墨辰丷原創
2018-06-01 17:25:5912999瀏覽

這篇文章主要介紹了PHP中spl_autoload_register()函數用法,結合實例形式分析了__autoload函數及spl_autoload_register函數的相關使用技巧,需要的朋友可以參考下

推薦手冊php完全自學手冊

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

一、__autoload

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

printit.class.php:

<?php
class PRINTIT {
 function doPrint() {
 echo &#39;hello world&#39;;
 }
}
?>

index.php

<?
function __autoload( $class ) {
 $file = $class . &#39;.class.php&#39;;
 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 . &#39;.class.php&#39;;
 if (is_file($file)) {
 require_once($file);
 }
}
spl_autoload_register( &#39;loadprint&#39; );
$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 . &#39;.class.php&#39;;
 if (is_file($file)) {
  require_once($file);
 }
 }
}
spl_autoload_register( array(&#39;test&#39;,&#39;loadprint&#39;) );
//另一种写法: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(&#39;LOAD&#39;, &#39;loadClass&#39;));
/**
*__autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法
* 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list
*/
spl_autoload_register( &#39;__autoload&#39;);

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

<?php
class 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, &#39;model&#39; ) );
    spl_autoload_register ( array ($this, &#39;helper&#39; ) );
    spl_autoload_register ( array ($this, &#39;controller&#39; ) );
    spl_autoload_register ( array ($this, &#39;library&#39; ) );
  }
  public function library($class) {
    set_include_path ( get_include_path () . PATH_SEPARATOR . &#39;/lib/&#39; );
    spl_autoload_extensions ( &#39;.library.php&#39; );
    spl_autoload ( $class );
  }
  public function controller($class) {
    $class = preg_replace ( &#39;/_controller$/ui&#39;, &#39;&#39;, $class );
    set_include_path ( get_include_path () . PATH_SEPARATOR . &#39;/controller/&#39; );
    spl_autoload_extensions ( &#39;.controller.php&#39; );
    spl_autoload ( $class );
  }
  public function model($class) {
    $class = preg_replace ( &#39;/_model$/ui&#39;, &#39;&#39;, $class );
    set_include_path ( get_include_path () . PATH_SEPARATOR . &#39;/model/&#39; );
    spl_autoload_extensions ( &#39;.model.php&#39; );
    spl_autoload ( $class );
  }
  public function helper($class) {
    $class = preg_replace ( &#39;/_helper$/ui&#39;, &#39;&#39;, $class );
    set_include_path ( get_include_path () . PATH_SEPARATOR . &#39;/helper/&#39; );
    spl_autoload_extensions ( &#39;.helper.php&#39; );
    spl_autoload ( $class );
  }
}
//call
autoloader::init ();
?>

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

相關文章推薦:    
1.PHP自動載入機制介紹-spl_autoload_register()函數,php類別自動載入
2.如何使用spl_autoload_register實作自動載入實例詳解
相關影片推薦:
1.獨孤九賤(4)_PHP影片教學

以上是關於PHP中spl_autoload_register()函數用法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn