|
一、php中實(shí)現(xiàn)自動(dòng)加載的方法
1.使用require,include,require_once,include_once手工進(jìn)行加載。
2.使用__autoload來進(jìn)行自動(dòng)加載
3.使用spl的autoload來實(shí)現(xiàn)自動(dòng)加載
手工加載的實(shí)現(xiàn):
當(dāng)需要加載的文件很少的時(shí)候我們可以使用第一個(gè)來完成。這樣做很簡(jiǎn)單也沒問題。
復(fù)制代碼 代碼如下:
require_once 'a.php';
require_once 'b.php';
require_once 'c.php';
但是當(dāng)需要加載文件很多的時(shí)候這樣做還行嗎?需要寫十個(gè),二十個(gè)require_once 或者更多的時(shí)候我們?cè)撛趺崔k?
這個(gè)時(shí)候我們可以使用__autoload方法來簡(jiǎn)化我們的代碼。
__autoload加載的實(shí)現(xiàn):
我們?cè)趖est目錄下創(chuàng)建一個(gè)in.php文件,內(nèi)容如下。
復(fù)制代碼 代碼如下:
echo '我是test下面的in.php<br />';
然后在test目錄下創(chuàng)建一個(gè)loader.php,內(nèi)容如下。
復(fù)制代碼 代碼如下:
// 需要重載__autoload方法,自定義包含類文件的路徑
function __autoload($classname)
{
$class_file = strtolower($classname).".php";
if (file_exists($class_file)){
require_once($class_file);
}
}
@$test = new in(); // 執(zhí)行到這里會(huì)輸出 <SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">我是test下面的in.php</SPAN>
沒問題,成功了!我們還可以創(chuàng)建其他的文件來進(jìn)行加載,但是當(dāng)需要的文件很多需要區(qū)分目錄的時(shí)候怎么辦?
這時(shí)我們需要修改loader.php可以使用映射來找到要加載的文件。
復(fù)制代碼 代碼如下:
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();
這種方法的好處就是類名和文件路徑只是用一個(gè)映射來維護(hù),所以當(dāng)文件結(jié)構(gòu)改變的時(shí)候,不需要修改類名,只需要將映射中對(duì)應(yīng)的項(xiàng)修改就好了。
但是__autoload在一個(gè)項(xiàng)目中只能使用一次,當(dāng)你的項(xiàng)目引用了別人的一個(gè)項(xiàng)目,你的項(xiàng)目中有一個(gè)__autoload,別人的項(xiàng)目也有一個(gè)__autoload,這樣兩個(gè)__autoload就沖突了.解決的辦法就是修改__autoload成為一個(gè),這無疑是非常繁瑣的,應(yīng)用場(chǎng)景單一。
spl的autoload加載實(shí)現(xiàn):
spl的autoload系列函數(shù)使用一個(gè)autoload調(diào)用堆棧,你可以使用spl_autoload_register注冊(cè)多個(gè)自定義的autoload函數(shù),應(yīng)用場(chǎng)景廣泛
•在test目錄下建立in.php,內(nèi)容如下
復(fù)制代碼 代碼如下:
<?php
class in {
public function index() {
echo '我是test下面的in.php';
}
}
?>
在test目錄下建立loader.php,內(nèi)容如下
復(fù)制代碼 代碼如下:
<?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ù)注冊(cè)到SPL __autoload函數(shù)棧中,修改loader.php
復(fù)制代碼 代碼如下:
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注冊(cè)多個(gè)自定義的autoload函數(shù)的應(yīng)用
首先在test目錄下建立mods文件夾并建立inmod.mod.php內(nèi)容如下:
復(fù)制代碼 代碼如下:
<?php
class inmod
{
function __construct()
{
echo '我是mods下的in';
}
}
然后在test目錄下建立libs文件夾并建立inlib.lib.php內(nèi)容如下:
復(fù)制代碼 代碼如下:
<?php
class inlib
{
function __construct()
{
echo '我是libs下的in';
}
}
最后在test目錄下建立loader.php內(nèi)容如下
復(fù)制代碼 代碼如下:
<?php
class Loader {
/**
* 自動(dòng)加載類
* @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>
php技術(shù):php自動(dòng)加載機(jī)制的深入分析,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。