最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php自動加載機制的深入分析

 更新時間:2013年06月08日 16:21:18   作者:  
本篇文章是對php自動加載機制進行了詳細的分析介紹,需要的朋友參考下

一、php中實現(xiàn)自動加載的方法
1.使用require,include,require_once,include_once手工進行加載。
2.使用__autoload來進行自動加載
3.使用spl的autoload來實現(xiàn)自動加載
手工加載的實現(xiàn):

當需要加載的文件很少的時候我們可以使用第一個來完成。這樣做很簡單也沒問題。

復制代碼 代碼如下:

require_once 'a.php';
require_once 'b.php';
require_once 'c.php';

但是當需要加載文件很多的時候這樣做還行嗎?需要寫十個,二十個require_once 或者更多的時候我們該怎么辦?

這個時候我們可以使用__autoload方法來簡化我們的代碼。

__autoload加載的實現(xiàn):
我們在test目錄下創(chuàng)建一個in.php文件,內(nèi)容如下。

復制代碼 代碼如下:

echo '我是test下面的in.php<br />';

然后在test目錄下創(chuàng)建一個loader.php,內(nèi)容如下。
復制代碼 代碼如下:

// 需要重載__autoload方法,自定義包含類文件的路徑 
function __autoload($classname) 

 $class_file = strtolower($classname).".php"; 
 if (file_exists($class_file)){ 
  require_once($class_file); 
 } 
}
@$test = new in(); // 執(zhí)行到這里會輸出 <SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">我是test下面的in.php</SPAN>

沒問題,成功了!我們還可以創(chuàng)建其他的文件來進行加載,但是當需要的文件很多需要區(qū)分目錄的時候怎么辦?

這時我們需要修改loader.php可以使用映射來找到要加載的文件。

復制代碼 代碼如下:

function __autoload($class_name) {
 $map = array(
  'index' => './include/index.php',
  'in' => './in.php'
 );

    if (file_exists($map[$class_name]) && isset($map[$class_name])) {
        require_once $map[$class_name];
    }
}
new index();


這種方法的好處就是類名和文件路徑只是用一個映射來維護,所以當文件結(jié)構(gòu)改變的時候,不需要修改類名,只需要將映射中對應(yīng)的項修改就好了。

但是__autoload在一個項目中只能使用一次,當你的項目引用了別人的一個項目,你的項目中有一個__autoload,別人的項目也有一個__autoload,這樣兩個__autoload就沖突了.解決的辦法就是修改__autoload成為一個,這無疑是非常繁瑣的,應(yīng)用場景單一。

spl的autoload加載實現(xiàn):
spl的autoload系列函數(shù)使用一個autoload調(diào)用堆棧,你可以使用spl_autoload_register注冊多個自定義的autoload函數(shù),應(yīng)用場景廣泛

•在test目錄下建立in.php,內(nèi)容如下  

復制代碼 代碼如下:

<?php
class in {
 public function index() {
  echo '我是test下面的in.php';
 }
}
?>

在test目錄下建立loader.php,內(nèi)容如下
復制代碼 代碼如下:

<?php
set_include_path("/var/www/test/"); //這里需要將路徑放入include
spl_autoload("in"); //尋找/var/www/test/in.php
$in = new in();
$in->index();

•spl_autoload_register將函數(shù)注冊到SPL __autoload函數(shù)棧中,修改loader.php
復制代碼 代碼如下:

function AutoLoad($class){
    if($class == 'in'){
        require_once("/var/www/test/in.php");
    }
}
spl_autoload_register('AutoLoad');
$a = new in();
$a->index();

•spl_autoload_register注冊多個自定義的autoload函數(shù)的應(yīng)用
首先在test目錄下建立mods文件夾并建立inmod.mod.php內(nèi)容如下:
復制代碼 代碼如下:

<?php
class inmod
{
 function __construct()
 {
  echo '我是mods下的in';
 }
}

然后在test目錄下建立libs文件夾并建立inlib.lib.php內(nèi)容如下:
復制代碼 代碼如下:

<?php
class inlib
{
 function __construct()
 {
  echo '我是libs下的in';
 }
}

最后在test目錄下建立loader.php內(nèi)容如下
復制代碼 代碼如下:

<?php
class Loader {
    /**
    * 自動加載類
    * @param $class 類名
    */
    public static function mods($class) {
        if($class){
   set_include_path( "/var/www/test/mods/" );
   spl_autoload_extensions( ".mod.php" );
   spl_autoload( strtolower($class) );
        }
    }
    public static function libs($class) {
  if($class){
   set_include_path( "/var/www/test/libs/" );
   spl_autoload_extensions( ".lib.php" );
   spl_autoload( strtolower($class) );
        }
    }
}
spl_autoload_register(array('Loader', 'mods'));
spl_autoload_register(array('Loader', 'libs'));
new inmod();//輸出<SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 14px">我是mods下的in</SPAN>
new inlib();//<SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">輸出</SPAN><SPAN style="FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 14px">我是libs下的in</SPAN>

相關(guān)文章

最新評論

梨树县| 宜州市| 万载县| 天等县| 大悟县| 南投市| 江川县| 松原市| 尉氏县| 图木舒克市| 青铜峡市| 瓮安县| 安徽省| 宕昌县| 稷山县| 鸡西市| 益阳市| 大埔县| 香格里拉县| 偃师市| 合山市| 兰考县| 凤城市| 吕梁市| 丰镇市| 阿瓦提县| 沂源县| 东阳市| 乐陵市| 右玉县| 惠州市| 卢龙县| 济阳县| 青阳县| 民乐县| 宁德市| 陇南市| 清河县| 天长市| 巴东县| 两当县|