//向自动加载目录列表中添加目录
function classpath($path){
//目录之间用冒号/分号分隔
set_include_path(get_include_path().PATH_SEPARATOR.$path);
}
//添加自动加载路径
cute_classpath('core/');
cute_classpath('app/');
cute_classpath('model/');
cute_classpath('controller/');
cute_classpath('libraries/');
//使用spl_autoload_register来注册我们自己的autoload函数
spl_autoload_register("my_autoload");
//重载autoload函数,以实现多目录自动加载
function my_autoload($className){
$directories = explode(PATH_SEPARATOR, get_include_path()) ;
$fileName = $className.CLASS_EXT;
foreach($directories as $dir){
$filePath = $dir.DIRECTORY_SEPARATOR.$fileName;
if( file_exists($filePath) ) {
include_once "$className.class.php";
break;
}
}
}
php-多目录自动加载
最新推荐文章于 2023-04-28 23:23:32 发布