为什么 PHP 在调用未定义函数时没有错误?
每当调用未定义的函数时,都不会记录任何错误。相反,脚本只是停止执行。更糟糕的是,如果我运行 php -l filename.php,它显示没有语法错误。我正在使用自定义错误处理函数,但即使是第一行也永远不会到达。
当调用未定义的函数时,如何让它运行我的错误处理程序?
我使用的是 PHP 5.3.2-1。以下是设置错误处理程序的代码:
error_reporting(-1);
$old_error_handler = set_error_handler( "userErrorHandler" );
set_exception_handler('exception_handler');
错误处理程序和异常处理程序均未到达,尽管它们确实可以处理其他错误。
我想要这个的原因是我最终必须在代码中放置调试语句以查看它在停止执行之前走了多远,与告诉我错误所在的文件和行号的错误消息相比,这是一个缓慢的过程。
Whenever there is a call to an undefined function, no errors are logged. Instead the script just stops executing. To make things worse, if I run php -l filename.php, It shows that there are no syntax errors. I am using a custom error handler function, but even the first line is never reached.
How can I get it to run my error handler when there is a call to an undefined function?
I am using PHP 5.3.2-1. Here is the code that is setting the error handler:
error_reporting(-1);
$old_error_handler = set_error_handler( "userErrorHandler" );
set_exception_handler('exception_handler');
Neither the error handler nor the exception handler are being reached, although they do work for other errors.
The reason I want this is I end up having to place debug statements in my code to see how far it gets before it stops executing which is a slow process compared to an error message that would tell me the file and line number where the error is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用户错误处理程序无法捕获致命错误。
请参阅 http://php.net/manual/en/function.set- error-handler.php
具体部分:
根据 PHP 手册页上的注释,一种解决方法是测试关闭函数中的错误:
Fatal errors can not be caught by a user error handler.
See http://php.net/manual/en/function.set-error-handler.php
Specifically the part:
And as per the comments on the PHP manual page, one work around is to test for errors in the shutdown function:
手册中有关于
set_error_handler 的相关部分()
。There is a relevant part in the manual for
set_error_handler()
.