PHP的autoLoad自動加載機制 的幾種方式

說說PHP的autoLoad自動加載機制

php的autoload大體可使用兩種方法:__autoload和spl方法。這兩種方法又各有不一樣的幾種使用方法php

__autoload的使用方法1:
最常常使用的就是這種方法,根據類名,找出類文件,而後require_one
json

複製代碼 代碼以下:框架


function __autoload($class_name) {
$path = str_replace('_', '/', $class_name);
require_once $path . '.php';
}
// 這裏會自動加載Http/File/Interface.php 文件
$a = new Http_File_Interface();
函數


這種方法的好處就是簡單易使用。固然也有缺點,缺點就是將類名和文件路徑強制作了約定,當修改文件結構的時候,就勢必要修改類名。

__autoload的使用方法2(直接映射法)
ui

複製代碼 代碼以下:this


$map = array(
'Http_File_Interface' => 'C:/PHP/HTTP/FILE/Interface.php'
);
function __autoload($class_name) {
if (isset($map[$class_name])) {
require_once $map[$class_name];
}
}
// 這裏會自動加載C:/PHP/HTTP/FILE/Interface.php 文件
$a = new Http_File_Interface();
spa



這種方法的好處就是類名和文件路徑只是用一個映射來維護,因此當文件結構改變的時候,不須要修改類名,只須要將映射中對應的項修改就行了。

這種方法相較於前面的方法缺點是當文件多了的時候這個映射維護起來很是麻煩,或許這時候你就會考慮使用json或者單獨一個文件來進行維護了。或許你會想到使用一個框架來維護或者創建這麼一個映射。

spl_autoload

__autoload的最大缺陷是沒法有多個autoload方法

好了, 想下下面的這個情景,你的項目引用了別人的一個項目,你的項目中有一個__autoload,別人的項目也有一個__autoload,這樣兩個__autoload就衝突了。解決的辦法就是修改__autoload成爲一個,這無疑是很是繁瑣的。

所以咱們急需使用一個autoload調用堆棧,這樣spl的autoload系列函數就出現了。你可使用spl_autoload_register註冊多個自定義的autoload函數

若是你的PHP版本大於5.1的話,你就可使用spl_autoload

先了解spl的幾個函數:


spl_autoload 是_autoload()的默認實現,它會去include_path中尋找$class_name(.php/.inc)
Spl_autoload實現自動加載:
code

複製代碼 代碼以下:圖片


/*http.php*/
<?php
class http
{
public function callname(){
echo "this is http";
}
}
/*test.php*/
<?php
set_include_path("/home/yejianfeng/handcode/"); //這裏須要將路徑放入include
spl_autoload("http"); //尋找/home/yejianfeng/handcode/http.php
$a = new http();
$a->callname();
it



Spl_autoload_register

將函數註冊到SPL __autoload函數棧中,直接看一個例子:

複製代碼 代碼以下:


/*http.php*/
<?php
class http
{
public function callname(){
echo "this is http";
}
}

/*test.php*/
<?php
spl_autoload_register(function($class){
if($class == 'http'){
require_once("/home/yejianfeng/handcode/http.php");
}
});

$a = new http();
$a->callname();



spl_autoload_call

調用spl_autoload_register中註冊的調用函數, 看下面的例子

複製代碼 代碼以下:


/*http.php*/
<?php
class http
{
public function callname(){
echo "this is http";
}
}
/*http2.php*/
<?php
class http
{
public function callname(){
echo "this is http2";
}
}

/*test.php*/
<?php
spl_autoload_register(function($class){
if($class == 'http'){
require_once("/home/yejianfeng/handcode/http.php");
}
if($class == 'http2'){
require_once("/home/yejianfeng/handcode/http2.php");
}
});
spl_auto_call('http2');
$a = new http();
$a->callname(); //這個時候會輸出"this is http2"


spl_auto_register這個函數使得咱們不使用__autoload,使用自定義的函數來進行自動加載成爲可能。這個方法如今是常常使用到的。
Zend的AutoLoader模塊就使用了這個方法。摘錄其中對應的代碼

複製代碼 代碼以下:

spl_autoload_register(array(__CLASS__, 'autoload')); public static function autoload($class) { ….. }

相關文章
相關標籤/搜索