php的自動載入機制_PHP教程

來源:互聯網
上載者:User
一、php中實現自動載入的方法

使用require,include,require_once,include_once手工進行載入。

使用__autoload來進行自動載入
使用spl的autoload來實現自動載入
手工載入的實現:

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


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

require_once 'a.php';
require_once 'b.php';
require_once 'c.php';
但是當需要負載檔案很多的時候這樣做還行嗎?需要寫十個,二十個require_once 或者更多的時候我們該怎麼辦?

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

__autoload載入的實現:

我們在test目錄下建立一個in.php檔案,內容如下。


[php]
echo '我是test下面的in.php
';

echo '我是test下面的in.php
';然後在test目錄下建立一個loader.php,內容如下。

[php]
// 需要重載__autoload方法,自訂包含類檔案的路徑
function __autoload($classname)
{
$class_file = strtolower($classname).".php";
if (file_exists($class_file)){
require_once($class_file);
}
}
@$test = new in(); // 執行到這裡會輸出 我是test下面的in.php

// 需要重載__autoload方法,自訂包含類檔案的路徑
function __autoload($classname)
{
$class_file = strtolower($classname).".php";
if (file_exists($class_file)){
require_once($class_file);
}
}
@$test = new in(); // 執行到這裡會輸出 我是test下面的in.php沒問題,成功了!我們還可以建立其他的檔案來進行載入,但是當需要的檔案很多需要區分目錄的時候怎麼辦?

這時我們需要修改loader.php可以使用映射來找到要載入的檔案。


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

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

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

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


spl的autoload載入實現:

spl的autoload系列函數使用一個autoload呼叫堆疊,你可以使用spl_autoload_register註冊多個自訂的autoload函數,應用情境廣泛


spl的自動載入的相關函數
spl_autoload 是_autoload()的預設實現,它會去include_path中尋找$class_name(.php/.inc) Spl_autoload實現自動載入:
在test目錄下建立in.php,內容如下


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

class in {
public function index() {
echo '我是test下面的in.php';
}
}
?> 在test目錄下建立loader.php,內容如下
[html]
set_include_path("/var/www/test/"); //這裡需要將路徑放入include
spl_autoload("in"); //尋找/var/www/test/in.php
$in = new in();
$in->index();

set_include_path("/var/www/test/"); //這裡需要將路徑放入include
spl_autoload("in"); //尋找/var/www/test/in.php
$in = new in();
$in->index();
spl_autoload_register將函數註冊到SPL __autoload函數棧中,修改loader.php
[php]
function AutoLoad($class){
if($class == 'in'){
require_once("/var/www/test/in.php");
}
}
spl_autoload_register('AutoLoad');
$a = new in();
$a->index();

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函數的應用
首先在test目錄下建立mods檔案夾並建立inmod.mod.php內容如下:
[php]
class inmod
{
function __construct()
{
echo '我是mods下的in';
}
}

class inmod
{
function __construct()
{
echo '我是mods下的in';
}
}
然後在test目錄下建立libs檔案夾並建立inlib.lib.php內容如下:
[php]
class inlib
{
function __construct()
{
echo '我是libs下的in';
}
}

class inlib
{
function __construct()
{
echo '我是libs下的in';
}
} 最後在test目錄下建立loader.php內容如下
[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();//輸出我是mods下的in
new inlib();//輸出我是libs下的in

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();//輸出我是mods下的in
new inlib();//輸出我是libs下的in

http://www.bkjia.com/PHPjc/477470.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/477470.htmlTechArticle一、php中實現自動載入的方法 使用require,include,require_once,include_once手工進行載入。 使用__autoload來進行自動載入 使用spl的autoload來實現...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.