下面是一段使用__autoload()的代码,供大家学习参考:
代码如下:
PHP
/**
* 自动加载相关类库文件
*/
function __autoload($classname){
if(substr($classname,-6)=="Action"){
include APP_PATH.'controllers/'.$classname.'.class.PHP';
}elseif(substr($classname,-5)=="Model"){
include APP_PATH.'models/'.$classname.'.class.PHP';
}elseif($classname=="Smarty"){
include SYSTEM_PATH.'smarty/Smarty.class.PHP';
}else{
include APP_PATH.'common/'.$classname.'.class.PHP';
}
}
?>
/**
* 自动加载相关类库文件
*/
function __autoload($classname){
if(substr($classname,-6)=="Action"){
include APP_PATH.'controllers/'.$classname.'.class.PHP';
}elseif(substr($classname,-5)=="Model"){
include APP_PATH.'models/'.$classname.'.class.PHP';
}elseif($classname=="Smarty"){
include SYSTEM_PATH.'smarty/Smarty.class.PHP';
}else{
include APP_PATH.'common/'.$classname.'.class.PHP';
}
}
?>
另一种包含路径的方法:
代码如下:
PHP
function __autoload($class_name) {
$path = str_replace('_',DIRECTORY_SEPARATOR,$class_name);
require_once $path.'.PHP';
}
?>
说明:将下划线转换为目录分隔符(DIRECTORY_SEPARATOR),这样做即可以有效管理库文件,又解决了跨平台的问题。 原文链接:https://www.f2er.com/php/26769.htmlfunction __autoload($class_name) {
$path = str_replace('_',DIRECTORY_SEPARATOR,$class_name);
require_once $path.'.PHP';
}
?>