PHP中spl_autoload_register()函數(shù)用法實(shí)例詳解
來源:易賢網(wǎng) 閱讀:1398 次 日期:2016-08-12 14:49:53
溫馨提示:易賢網(wǎng)小編為您整理了“PHP中spl_autoload_register()函數(shù)用法實(shí)例詳解”,方便廣大網(wǎng)友查閱!

本文實(shí)例分析了PHP中spl_autoload_register()函數(shù)用法。分享給大家供大家參考,具體如下:

在了解這個(gè)函數(shù)之前先來看另一個(gè)函數(shù):__autoload。

一、__autoload

這是一個(gè)自動(dòng)加載函數(shù),在PHP5中,當(dāng)我們實(shí)例化一個(gè)未定義的類時(shí),就會(huì)觸發(fā)此函數(shù)。看下面例子:

printit.class.php:

<?php

class 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();?>

運(yùn)行index.php后正常輸出hello world。在index.php中,由于沒有包含printit.class.php,在實(shí)例化printit時(shí),自動(dòng)調(diào)用__autoload函數(shù),參數(shù)$class的值即為類名printit,此時(shí)printit.class.php就被引進(jìn)來了。

在面向?qū)ο笾羞@種方法經(jīng)常使用,可以避免書寫過多的引用文件,同時(shí)也使整個(gè)系統(tǒng)更加靈活。

二、spl_autoload_register()

再看spl_autoload_register(),這個(gè)函數(shù)與__autoload有與曲同工之妙,看個(gè)簡(jiǎn)單的例子:

<?

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函數(shù)。但是loadprint不會(huì)像__autoload自動(dòng)觸發(fā),這時(shí)spl_autoload_register()就起作用了,它告訴PHP碰到?jīng)]有定義的類就執(zhí)行l(wèi)oadprint()。

spl_autoload_register() 調(diào)用靜態(tài)方法

<?

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 — 注冊(cè)__autoload()函數(shù)

說明

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ù)。如果沒有提供任何參數(shù),則自動(dòng)注冊(cè)autoload的默認(rèn)實(shí)現(xiàn)函數(shù)spl_autoload()。

返回值

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

注:SPL是Standard PHP Library(標(biāo)準(zhǔn)PHP庫)的縮寫。它是PHP5引入的一個(gè)擴(kuò)展庫,其主要功能包括autoload機(jī)制的實(shí)現(xiàn)及包括各種Iterator接口或類。SPL autoload機(jī)制的實(shí)現(xiàn)是通過將函數(shù)指針autoload_func指向自己實(shí)現(xiàn)的具有自動(dòng)裝載功能的函數(shù)來實(shí)現(xiàn)的。SPL有兩個(gè)不同的函數(shù)spl_autoload, spl_autoload_call,通過將autoload_func指向這兩個(gè)不同的函數(shù)地址來實(shí)現(xiàn)不同的自動(dòng)加載機(jī)制。

classLOAD

{

 staticfunctionloadClass($class_name)

  {

    $filename= $class_name.".class.php";

 $path= "include/".$filename

    if(is_file($path)) returninclude$path;

  }

}

/**

 * 設(shè)置對(duì)象的自動(dòng)載入

 * spl_autoload_register — Register given function as __autoload() implementation

 */

spl_autoload_register(array('LOAD', 'loadClass'));

/**

*__autoload 方法在 spl_autoload_register 后會(huì)失效,因?yàn)?autoload_func 函數(shù)指針已指向 spl_autoload 方法

* 可以通過下面的方法來把 _autoload 方法加入 autoload_functions list

*/

spl_autoload_register( '__autoload');

如果同時(shí)用spl_autoload_register注冊(cè)了一個(gè)類的方法和__autoload函數(shù),那么,會(huì)根據(jù)注冊(cè)的先后,如果在第一個(gè)注冊(cè)的方法或函數(shù)里加載了類文件,就不會(huì)再執(zhí)行第二個(gè)被注冊(cè)的類的方法或函數(shù)。反之就會(huì)執(zhí)行第二個(gè)被注冊(cè)的類的方法或函數(shù)。

<?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, '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 );

  }

}

//call

autoloader::init ();

?>

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:PHP中spl_autoload_register()函數(shù)用法實(shí)例詳解
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn)

版權(quán)所有:易賢網(wǎng)