使用spl_autoload()无法加载类
-
02-10-2019 - |
题
我正在使用SPL自动加载功能,并且似乎缺少重要的东西,因为我目前无法使其工作。这是我目前正在使用的片段:
// ROOT_DIRECTORY translates to /home/someuser/public_html/subdomains/test
define('ROOT_DIRECTORY', realpath(dirname(__FILE__)));
define('INCLUDE_DIRECTORY', ROOT_DIRECTORY . '/includes/classes/');
set_include_path(get_include_path() . PATH_SEPARATOR . INCLUDE_DIRECTORY);
spl_autoload_extensions('.class.php, .interface.php, .abstract.php');
spl_autoload_register();
当我 echo get_include_path()
我确实得到了我期望的道路:
// Output echo get_include_path();
.:/usr/lib/php:/usr/local/lib/php:/home/someuser/public_html/subdomains/test/includes/classes/
但是,当我运行代码时,我会收到此错误消息:
致命错误:spl_autoload()[function.spl-autoload]:不能在/home/someuser/public_html/subdomains/test/contact.php中加载类请求。
request.class.php是 确实 在/home/home/someuser/public_html/subdomains/test/incluct/class/directory中。
我想念什么?
解决方案
有评论(匿名) http://www.php.net/manual/en/function.spl-autoload-register.php#96804 这可能适用于您的问题:SPL_AUTOLOAD_REGISTER()似乎对骆驼效果不佳,在您的情况下,可能正在尝试找到request.class.php而不是请求...
其他提示
如果您期望他们,那么应该是班级的道路似乎与道路不符。比较
.:/usr/lib/php:/usr/local/lib/php:/home/someuser/public_html/subdomains/test/includes/classes/
和
/home/someuser/public_html/subdomains/test/
不同之处在于,您的班级不在 includes/classes/
如您的SPL所需要的那样,但上面几个目录。
我收到了类似的错误消息,但我的问题有所不同。我的错误消息看起来像
PHP Fatal error: spl_autoload(): Class Lib\Lib\Regex could not be loaded in /dir1/dir2/lib/regex.php on line 49
原来我忘了去除 Lib\
从 Lib\Regex
在Regex类定义本身内。我有以下内容:
namespace Lib;
class Regex {
...
public static function match($pattern, $str) {
$regex = new Lib\Regex($pattern);
...
}
}
不隶属于 StackOverflow