php自動(dòng)加載方式集合

字號(hào):


    這篇文章主要針對(duì)php自動(dòng)加載方式進(jìn)行大集合,一種是常規(guī)加載方式,另一種是__autoload()自動(dòng)加載,想知道第三種方式請(qǐng)閱讀下文
    php加載文件方式:
    1、include,include_once,requice,requice_one常規(guī)加載
    2、__autoload()
    3、spl_autoload_register()
    常規(guī)加載方式
    假設(shè)我們有一個(gè)類文件A.php,里面定義了一個(gè)名字為A的類:
    <?php  
    class A {  
      public function __construct() {  
        echo 'Got it.';  
      }  
    }
    然后我們有一個(gè)index.php需要用到這個(gè)類A,常規(guī)的寫法就是
    <?php  
      require('A.php');  
      $a = new A();
    但是有一個(gè)問題就是,假如我們的index.php需要包含的不只是類A,而是需要很多類,這樣子就必須寫很多行require語(yǔ)句,有時(shí)候也會(huì)讓人覺得不爽。
    autoload()自動(dòng)加載
    不過(guò)在php5之后的版本,我們就不再需要這樣做了。
    在php5中,試圖使用尚未定義的類時(shí)會(huì)自動(dòng)調(diào)用__autoload函數(shù),所以我們可以通過(guò)編寫__autoload函數(shù)來(lái)讓php自動(dòng)加載類,而不必寫一個(gè)長(zhǎng)長(zhǎng)的包含文件列表。
    例如在上面那個(gè)例子中,index.php可以這樣寫:
    <?php  
    function __autoload($class){  
      $file = $class . '.php';  
      if (is_file($file)) {  
        require_once($file);  
      }  
    }  
    $a = new A();
    當(dāng)然上面只是最簡(jiǎn)單的示范,__autoload只是去include_path尋找類文件并加載,我們可以根據(jù)自己的需要定義__autoload加載類的規(guī)則。注意:由于__autoload()是個(gè)函數(shù),只能存在一次。
    spl_autoload_register()自動(dòng)加載
    但現(xiàn)在問題來(lái)了,如果在一個(gè)系統(tǒng)的實(shí)現(xiàn)中,如果需要使用很多其它的類庫(kù),這些類庫(kù)可能是由不同的開發(fā)人員編寫的,其類名與實(shí)際的磁盤文件的映射規(guī)則不盡相同。這時(shí)如果要實(shí)現(xiàn)類庫(kù)文件的自動(dòng)加載,就必須在__autoload()函數(shù)中將所有的映射規(guī)則全部實(shí)現(xiàn),這樣的話__autoload()函數(shù)有可能 會(huì)非常復(fù)雜,甚至無(wú)法實(shí)現(xiàn)。最后可能會(huì)導(dǎo)致__autoload()函數(shù)十分臃腫,這時(shí)即便能夠?qū)崿F(xiàn),也會(huì)給將來(lái)的維護(hù)和系統(tǒng)效率帶來(lái)很大的負(fù)面影響。在這種情況下,難道就沒有更簡(jiǎn)單清晰的解決辦法了吧?答案當(dāng)然是:NO!
    spl_autoload_register() 滿足了此類需求。 它實(shí)際上創(chuàng)建了 autoload 函數(shù)的隊(duì)列,按定義時(shí)的順序逐個(gè)執(zhí)行。相比之下, __autoload() 只可以定義一次。
    bool spl_autoload_register ([ callable $autoload_function [, bool $throw = true [, bool $prepend = false ]]] )
    我們繼續(xù)改寫上面那個(gè)例子:
    <?php  
    function loader($class){  
      $file = $class . '.php';  
      if (is_file($file)) {  
        require_once($file);  
      }  
    }  
    spl_autoload_register('loader');  
    $a = new A();
    或者直接使用匿名函數(shù):
    <?php  
    spl_autoload_register(function($file){
      $file = $class . '.php';  
      if (is_file($file)) {  
        require_once($file);  
      }
    });  
    $a = new A();
    這樣子也是可以正常運(yùn)行的,這時(shí)候php在尋找類的時(shí)候就沒有調(diào)用__autoload而是調(diào)用我們自己定義的函數(shù)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')); 
    //spl_autoload_register(array(__CLASS__, 'loadClass')); 
    //spl_autoload_register(array($this, 'loadClass')); 
    $a = new A();
    更多示例
    autoload.php
    <?php
    define('SDK_PATH', __DIR__);
    require_once SDK_PATH . '/common/functions.php';
    require_once SDK_PATH . '/common/config.php';
    spl_autoload_register(function ($class) {
      if (false !== stripos($class, 'YJC\Wechat')) {
        require_once __DIR__ . '/' . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 10)) . '.php';
      }
    });
    /*
    function __autoload($class){
      if (false !== stripos($class, 'YJC\Wechat')) {
        require_once __DIR__ . '/' . str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 10)) . '.php';
      }
    }
    */
    建議入口文件里養(yǎng)成定義絕對(duì)地址SDK_PATH的習(xí)慣,這樣require不會(huì)出錯(cuò)。
    以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。