The example in this article describes the implementation method of PHP class autoloader. Share it with everyone for your reference. The details are as follows:
The autoload here is compatible with the following formats:
Cache_File_Json
class_xxx.php
xxx.class.php
xxx.php
The php code is as follows:
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; }
I hope this article will be helpful to everyone’s PHP programming design.