<output id="r87xx"></output>
    1. 
      
      <mark id="r87xx"><thead id="r87xx"><input id="r87xx"></input></thead></mark>
        •   

               當(dāng)前位置:首頁>軟件介紹>php主動加載 查詢:
               
          php主動加載

                  php中有兩種自動加載機制函數(shù)

                  [php]

                  __autoload();

                  spl_autoload_register();

                  1. __autoload()

                  可以將需要使用類的時候把文件加載到程序中

                   [php]

                  <?php

                  function __autoload($className) {

                  if (file_exists($className . '.php')) {

                  include $className . '.php';//可細(xì)化

                  } else {

                  echo $className . '.php is not exists.';

                  exit;

                  }

                  }

                  $indexController = new IndexController();

                  在程序的運行過程中,php會檢測這個$className類是否已經(jīng)加載,如果沒有加載會去執(zhí)行__autoload(),再去加載$className這個類。在實例化類的對象、訪問類中的靜態(tài)變量和方法等都會去檢測類是否已經(jīng)加載,是否有定義__autoload()函數(shù),如果都沒有就會報錯。

                  在復(fù)雜點的系統(tǒng)中,用__autoload()來實現(xiàn)類的自動加載可能會很復(fù)

                  雜。

                  2. spl_autoload_register()

                  [php]

                  <?php

                  spl_autoload_register();

                  $index = new Index();

                  spl_autoload_register()函數(shù)中沒有參數(shù),則會自動默認(rèn)實現(xiàn)void

                  spl_autoload ( string $class_name [,string $file_extensions ] )

                  函數(shù),默認(rèn)支持.php和.ini

                  [php]

                  function load1($className) {

                  //include

                  }

                  function load2($className) {

                  //include

                  }

                  spl_autoload_register('load1');//注冊到<span

                  class="methodname">spl_autoload_functions</span>

                  spl_autoload_register('load2');

                  $index = new Index();

                  會先通過load1去加載類,如果load1中沒有,再通過load2去加載,如果還有以次類推。  

                  實現(xiàn)一個自動加載方法比較多,這舉例一個

                  [php]

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

                  ?>

                  也可以根據(jù)自己的需要來設(shè)計實現(xiàn)

                  


          PHP入門的學(xué)習(xí)方法php語言入門教程
          對php編程語言的理解PHP語法總結(jié)
          PHP編程風(fēng)格PHP基本語法
          PHP簡單介紹php程序員
          PHP緩存服務(wù)器之RedisPHP教學(xué)之PHP基本語法
          PHP程序員如何不斷學(xué)習(xí)?PHP二進(jìn)制加密與解密的解決辦法
          PHP語法入門PHP平臺的缺陷
          如何成為PHP高手PHP入門學(xué)習(xí)基礎(chǔ)教程
          信息發(fā)布:廣州名易軟件有限公司 http://m.jetlc.com
          • 勁爆價:
            不限功能
            不限用戶
            1998元/年

          • 微信客服

            <output id="r87xx"></output>
          1. 
            
            <mark id="r87xx"><thead id="r87xx"><input id="r87xx"></input></thead></mark>
              • 超色97| 黄色在线免费观看网站 | 欧美视频日韩视频 | 91视频精品 | 成人Av无码一区二区三区 | 欧美第8页 | 欧美视频a| 91传媒在线观看网站 | 欧美久久性爱免费视频 | 免费操 |