简体   繁体   English

PHP E_STRICT和__autoload()

[英]PHP E_STRICT and __autoload()

I am using __autoload() which simply looks like this: 我正在使用__autoload(),它看起来像这样:

function __autoload($class_name) {
  require_once($class_name . '.class.php');
}

When the error reporting is E_ALL it works fine. 当错误报告为E_ALL它将正常工作。 The class is loaded, and the script runs without errors. 该类已加载,脚本运行无误。

When the error reporting is E_ALL | E_STRICT 错误报告为E_ALL | E_STRICT E_ALL | E_STRICT , no pages work, I simply get: E_ALL | E_STRICT ,没有页面E_ALL | E_STRICT ,我只得到:

"Fatal error: Class 'NameOfClass' not found in \path\to\current\script on line 0"

Why? 为什么? Is this expected behaviour when using __autoload() or is it a problem with my script? 使用__autoload()时这是预期的行为还是我的脚本有问题?

The problem was that I was turning all my errors into exceptions with custom error handlers. 问题是我正在使用自定义错误处理程序将所有错误转换为异常。

In STRICT mode, the class being included by autoload was giving a minor error about code usage. 在STRICT模式下,自动加载包含的类在代码使用方面给出了一个小错误。 But this was being turned into an exception. 但这被变成一个例外。

autoload ignores exceptions so that the next autoload (if multiple have been registered) can try to load the class. autoload会忽略异常,以便下一次自动加载(如果已注册多个)可以尝试加载该类。

Therefore the error in my class file was never shown, but prevented the class from existing, giving the mysterious error on line 0 problem. 因此,我的类文件中的错误从未显示过,但是阻止了该类的存在,从而在第0行问题上产生了神秘的错误。

Disabling my custom error handler meant PHP printed an error (which I can see) rather than throwing an exception (which gets suppressed by autoload), and then I could see the real cause of the problem and fix it. 禁用我的自定义错误处理程序意味着PHP会打印一个错误(我可以看到),而不是抛出一个异常(该错误会被自动加载抑制),然后我可以查看问题的真正原因并进行修复。

好吧,如果您包含一个文件,但这样做后仍未加载该类-显然它将引发错误。

Maybe you can try using spl_autoload_register instead. 也许您可以尝试使用spl_autoload_register代替。 I've used that in an E_STRICT environment with out errors.. though technically what you're writing should work as well 我已经在没有错误的E_STRICT环境中使用了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM