国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

php自動加載機制的深入分析

2024-05-04 23:10:27
字體:
供稿:網(wǎng)友

一、php中實現(xiàn)自動加載的方法
1.使用require,include,require_once,include_once手工進行加載。
2.使用__autoload來進行自動加載
3.使用spl的autoload來實現(xiàn)自動加載
手工加載的實現(xiàn):

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

復(fù)制代碼 代碼如下:


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


但是當需要加載文件很多的時候這樣做還行嗎?需要寫十個,二十個require_once 或者更多的時候我們該怎么辦?

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

__autoload加載的實現(xiàn):
我們在test目錄下創(chuàng)建一個in.php文件,內(nèi)容如下。

復(fù)制代碼 代碼如下:


echo '我是test下面的in.php<br />';


然后在test目錄下創(chuàng)建一個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í)行到這里會輸出 <SPAN>我是test下面的in.php</SPAN>


沒問題,成功了!我們還可以創(chuàng)建其他的文件來進行加載,但是當需要的文件很多需要區(qū)分目錄的時候怎么辦?

這時我們需要修改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();


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

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

spl的autoload加載實現(xiàn):
spl的autoload系列函數(shù)使用一個autoload調(diào)用堆棧,你可以使用spl_autoload_register注冊多個自定義的autoload函數(shù),應(yīng)用場景廣泛

php自動加載機制的深入分析

•在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ù)注冊到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注冊多個自定義的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 {
/**
* 自動加載類
* @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>


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 青冈县| 光泽县| 伽师县| 门头沟区| 阿克苏市| 惠水县| 瑞昌市| 洪湖市| 新营市| 松溪县| 左权县| 罗山县| 汉川市| 南汇区| 庆云县| 朝阳市| 瓮安县| 南投市| 嘉兴市| 平湖市| 景德镇市| 确山县| 阜平县| 株洲市| 神农架林区| 图片| 铅山县| 福州市| 高青县| 同江市| 新绛县| 剑川县| 方城县| 朝阳市| 安化县| 陆川县| 洪江市| 惠东县| 封丘县| 南城县| 休宁县|