0712-2888027 189-8648-0214
微信公眾號

孝感風(fēng)信網(wǎng)絡(luò)科技有限公司微信公眾號

當前位置:主頁 > 技術(shù)支持 > PHP > php使用魔術(shù)函數(shù)__autoload自動引入實例化對象文件

php使用魔術(shù)函數(shù)__autoload自動引入實例化對象文件

時間:2024-09-16來源:風(fēng)信官網(wǎng) 點擊: 670次
php使用魔術(shù)函數(shù)__autoload自動引入實例化對象文件,示例代碼如下:
 
<?php 
 
//魔術(shù)函數(shù) __autoload
function __autoload($className)  //  Libs\A      =>   ./Libs/A.php
{
//將反斜線 \  替換成 /
$class = str_replace('\\','/',$className);  //  Libs/A  =>  ./Libs/A.php
//拼接文件的路徑
$path = './'.$class.'.php';
//檢測文件是否存在
if(file_exists($path)) {
//引入該類文件
include $path;
}
}
 
//如果當前類的空間路徑跟文件所處的路徑保持一致的話 很容易獲取到當前類文件的位置
// $obj = new \Org\Obj;
 
//實例化a對象
// $a = new \Libs\A;
 
//實例化B對象
// $b = new \Org\B;
 
//實例化D對象
// $d = new \A\B\C\D;
var_dump($d);
 
 
 ?>
欄目列表
推薦內(nèi)容
熱點內(nèi)容
展開