PHP自动加载奇数

|
function __autoload($class_name) {
    echo(\"Attempting autoload \");
    if (substr($class_name, -6) == \"Mapper\") {
        $file = \'mappers/\'.$class_name.\'.php\';
        echo \"Will autoload $file \";
        include_once($file);
    }
}
__autoload(\"UserMapper\");
$user = new UserMapper($adapter);

die(\"done\");
结果: 尝试自动加载将自动加载mappers / UserMapper.php完成
function __autoload($class_name) {
    echo(\"Attempting autoload \");
    if (substr($class_name, -6) == \"Mapper\") {
        $file = \'mappers/\'.$class_name.\'.php\';
        echo \"Will autoload $file \";
        include_once($file);
    }
}
//__autoload(\"UserMapper\");
$user = new UserMapper($adapter);

die(\"done\");
(我刚刚注释了对__autoload()的手动调用。) 结果: 致命错误:在第13行的C:\\ Program Files \\ EasyPHP-5.3.5.0 \\ www \\ proj \\ 29letters \\ login.php中找不到类\'UserMapper \' 有任何想法吗? 是的,我正在运行PHP 5.3.5     
已邀请:
不确定为什么您的示例不起作用,应按照手册进行操作。 您是否尝试过使用spl_autoload_register注册自动加载器功能?     
您设定了适当的
include_path
吗?您正在使用相对路径来包含该类的文件。请尝试使用绝对路径。
$dir  = __DIR__ . \'/../path/to/mappers\';
$file = $dir . \'/\' . $class_name . \'.php\';
require $file;
要么
// do this outside of __autoload
set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . \'/../path/to/mappers\';

// inside __autoload
$file = $class_name . \'.php\';
require $file;
    

要回复问题请先登录注册