php類自動加載器實現(xiàn)方法
更新時間:2015年07月28日 11:51:17 作者:yak
這篇文章主要介紹了php類自動加載器實現(xiàn)方法,涉及php針對文件的讀取、判斷及字符串操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了php類自動加載器實現(xiàn)方法。分享給大家供大家參考。具體如下:
這里autoload 可兼容以下格式:
Cache_File_Json
class_xxx.php
xxx.class.php
xxx.php
php代碼如下:
function __autoload($className){
$dirs=explode('_',$className);
$fileName=array_pop($dirs);
//print_r($dirs);
$filePath=$fileName;
if(is_array($dirs) && (count($dirs) > 0)){
//echo '\n---\n'; print_r($dirs);
$dirPath='';
foreach ($dirs as $dir){
if($dir){
$dirPath.=strtolower($dir).DIRECTORY_SEPARATOR;
}
}
$filePath=$dirPath.$fileName.'.php';
}else {
if( file_exists('class_'.$fileName.'.php')){
$filePath='class_'.$fileName.'.php';
}else {
if( file_exists($fileName.'.class.php')){
$filePath=$fileName.'.class.php';
} else {
$filePath=$fileName.'.php';
}
}
}
//var_dump($filePath);
require $filePath;
}
希望本文所述對大家的php程序設(shè)計有所幫助。
相關(guān)文章
PHP進程通信基礎(chǔ)之信號量與共享內(nèi)存通信
這篇文章主要介紹了PHP進程通信基礎(chǔ)知識中的信號量與共享內(nèi)存通信的相關(guān)資料,有需要的小伙伴可以查看下2017-02-02
php上傳文件并存儲到mysql數(shù)據(jù)庫的方法
這篇文章主要介紹了php上傳文件并存儲到mysql數(shù)據(jù)庫的方法,以完整實例形式較為詳細的分析了php操作文件上傳與數(shù)據(jù)庫存儲的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03

