根据 对此答案的评论 可以通过 set_error_handler() 捕获的noreferrer">关闭函数.
According to the comment on this answer it is possible to catch Fatal Errors through a shutdown function which cannot be caught using set_error_handler()
.
但是,我无法确定如何确定关闭是由于致命错误还是由于脚本结束.
However, I couldn't find out how to determine if the shutdown has occured due to a fatal error or due to the script reaching its end.
此外,调试回溯功能似乎在关闭功能中已失效,因此记录发生致命错误的堆栈跟踪变得毫无价值.
Additionally, the debug backtrace functions seem to be defunct in the shutdown function, making it pretty worthless for logging the stack trace where the Fatal Error occured.
所以我的问题是:在保持创建适当回溯的能力的同时,对致命错误(尤其是未定义的函数调用)做出反应的最佳方法是什么?
So my question is: what's the best way to react on Fatal Errors (especially undefined function calls) while keeping the ability to create a proper backtrace?
这对我有用:
function shutdown() {
$error = error_get_last();
if ($error['type'] === E_ERROR) {
// fatal error has occured
}
}
register_shutdown_function('shutdown');
spl_autoload_register('foo');
// throws a LogicException which is not caught, so triggers a E_ERROR
但是,您可能已经知道了,但只是为了确保:您无法以任何方式从 E_ERROR 中恢复.
However, you probably know it already, but just to make sure: you can't recover from a E_ERROR in any way.
至于回溯,你不能...... :(在大多数致命错误的情况下,尤其是未定义函数错误,你并不真正需要它.精确定位文件/行它发生的地方就足够了.在这种情况下,回溯无关紧要.
As for the backtrace, you can't... :( In most cases of a fatal error, especially Undefined function errors, you don't really need it. Pinpointing the file/line where it occured is enough. The backtrace is irrelevant in that case.
这篇关于使用 register_shutdown_function() 处理 PHP 中的致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!