溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php自動(dòng)加載機(jī)制的實(shí)現(xiàn)方法

發(fā)布時(shí)間:2021-07-24 11:45:29 來(lái)源:億速云 閱讀:133 作者:chen 欄目:開(kāi)發(fā)技術(shù)

本篇內(nèi)容主要講解“php自動(dòng)加載機(jī)制的實(shí)現(xiàn)方法”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“php自動(dòng)加載機(jī)制的實(shí)現(xiàn)方法”吧!

一、php中實(shí)現(xiàn)自動(dòng)加載的方法
1.使用require,include,require_once,include_once手工進(jìn)行加載。
2.使用__autoload來(lái)進(jìn)行自動(dòng)加載
3.使用spl的autoload來(lái)實(shí)現(xiàn)自動(dòng)加載
手工加載的實(shí)現(xiàn):

當(dāng)需要加載的文件很少的時(shí)候我們可以使用第一個(gè)來(lái)完成。這樣做很簡(jiǎn)單也沒(méi)問(wè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方法來(lái)簡(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 >我是test下面的in.php</SPAN>


沒(méi)問(wèn)題,成功了!我們還可以創(chuàng)建其他的文件來(lái)進(jìn)行加載,但是當(dāng)需要的文件很多需要區(qū)分目錄的時(shí)候怎么辦?

這時(shí)我們需要修改loader.php可以使用映射來(lái)找到要加載的文件。

復(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è)映射來(lái)維護(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è),這無(wú)疑是非常繁瑣的,應(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)景廣泛

php自動(dòng)加載機(jī)制的實(shí)現(xiàn)方法

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

到此,相信大家對(duì)“php自動(dòng)加載機(jī)制的實(shí)現(xiàn)方法”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI