[英]Using spl_autoload() not able to load class
I'm playing around with the SPL autoload functionality and seem to be missing something important as I am currently unable to get it to work. 我正在玩SPL自动加载功能,似乎缺少重要的东西,因为我目前无法让它工作。 Here is the snippet I am currently using:
这是我目前使用的片段:
// 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();
When I echo get_include_path()
I do get the path I expected: 当我
echo get_include_path()
我得到了我期望的路径:
// Output echo get_include_path();
.:/usr/lib/php:/usr/local/lib/php:/home/someuser/public_html/subdomains/test/includes/classes/
However when I run the code I get this error message: 但是,当我运行代码时,我收到此错误消息:
Fatal error: spl_autoload() [function.spl-autoload]: Class Request could not be loaded in /home/someuser/public_html/subdomains/test/contact.php on line 5
致命错误:spl_autoload()[function.spl-autoload]:无法在第5行的/home/someuser/public_html/subdomains/test/contact.php中加载类请求
Request.class.php is definitely in the /home/someuser/public_html/subdomains/test/includes/classes/ directory. Request.class.php 肯定在/ home / someuser / public_html / subdomains / test / includes / classes /目录中。
What am I missing? 我错过了什么?
http://www.php.net/manual/en/function.spl-autoload-register.php#96804上有一条评论(匿名)可能适用于您的问题:spl_autoload_register()似乎不是很好玩使用camelcase,在你的情况下可能试图找到request.class.php而不是Request ...
I got a similar error message but my issue was different. 我收到了类似的错误消息,但我的问题不同了。 My error message looked like
我的错误消息看起来像
PHP Fatal error: spl_autoload(): Class Lib\Lib\Regex could not be loaded in /dir1/dir2/lib/regex.php on line 49
It turned out I forgot to remove the Lib\\
from Lib\\Regex
inside the Regex class definition itself. 事实证明我忘了从Regex类定义本身中的
Lib\\Regex
删除Lib\\
。 I had something like the following: 我有类似以下内容:
namespace Lib;
class Regex {
...
public static function match($pattern, $str) {
$regex = new Lib\Regex($pattern);
...
}
}
The path where the class is supposed to be seems not to match the path were you expect them. 类的路径似乎与您期望的路径不匹配。 Compare
相比
.:/usr/lib/php:/usr/local/lib/php:/home/someuser/public_html/subdomains/test/includes/classes/
with 同
/home/someuser/public_html/subdomains/test/
The difference is, that your class is not in includes/classes/
as your SPL requires it but a few directories above. 不同之处在于,您的类不在
includes/classes/
因为您的SPL需要它,但上面有几个目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.