程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 說說PHP的autoLoad自動加載機制

說說PHP的autoLoad自動加載機制

編輯:關於PHP編程

__autoload的使用方法1:
最經常使用的就是這種方法,根據類名,找出類文件,然後require_one
復制代碼 代碼如下:
function __autoload($class_name) {
$path = str_replace('_', '/', $class_name);
require_once $path . '.php';
}
// 這裡會自動加載Http/File/Interface.php 文件
$a = new Http_File_Interface();

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

__autoload的使用方法2(直接映射法)
復制代碼 代碼如下:
$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();


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

這種方法相較於前面的方法缺點是當文件多了的時候這個映射維護起來非常麻煩,或許這時候你就會考慮使用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實現自動加載:
復制代碼 代碼如下:
/*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();


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)
{
…..

}


建議參考文章:
關於zend的autoload機制,前面有篇文章http://www.jb51.net/article/31399.htm 有詳細分析。
關於autoload的自動加載機制,這篇文章http://www.jb51.net/article/31279.htm說得非常詳細,從底層開始分析。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved