這篇文章主要介紹了php類自動加載器實現方法,涉及php針對文件的讀取、判斷及字符串操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了php類自動加載器實現方法。分享給大家供大家參考。具體如下:
這里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程序設計有所幫助。
新聞熱點
疑難解答