开发者

PHP E_STRICT and __autoload()

I am using __autoload() which simply looks like this:

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

When the error reporting is E_ALL it works fine. The class is loaded, and the script runs without errors.

When the error reporting is E_ALL | E_STRICT, no pages work, I simply get:

"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 scrip开发者_如何学Ct?


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. 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.

Therefore the error in my class file was never shown, but prevented the class from existing, giving the mysterious error on line 0 problem.

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.


Well, if you include a file, and the class is still not loaded after you do - obviously it will throw an error.


Maybe you can try using spl_autoload_register instead. I've used that in an E_STRICT environment with out errors.. though technically what you're writing should work as well

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜