龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php类自动加载器

时间:2014-06-27 15:16来源:网络整理 作者:网络 点击:
分享到:
php类自动加载器 兼容以下格式 Cache_File_Json class_xxx.php xxx.class.php xxx.php```{.php}function __autoload($className){$dirs=explode(#39;_#39;,$className);$fileName=array_pop($dirs);//

兼容以下格式
Cache_File_Json
class_xxx.php
xxx.class.php
xxx.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;
}
精彩图集

赞助商链接