2

根据我对 PHP 文档的理解,设置自定义错误处理程序(无法捕获所有错误)会停用默认的 php 错误处理程序(我猜它可以捕获所有错误:)。

如果这是正确的,是否有任何设置自定义错误处理程序的目的?我总是保留error_reporting(-1),所以无论如何我都应该在错误日志中得到所有错误,对吧?

至于用户体验,我看不到以自定义方式显示这些错误的目的——你为什么希望用户看到它们?在生产中,我总是将 display_errors 设置为关闭。

4

3 回答 3

4

In production you probably want to tell the user something when your site crashes, and set_error_handler provides a way of rendering pretty error messages while preventing the user from seeing ugly things like code and line numbers. It catches any runtime errors that the regular PHP error handler catches.

There is no reason to avoid using set_error_handler, and it's certainly preferable to simply displaying nothing when an error occurs.

于 2011-06-16T13:39:37.703 回答
3

Setting a custom error handler is for setting a catch-all handler that takes care of the errors that aren't handled elsewhere. It can catch all errors, except for the ones where your script isn't ran at all (such as fatal syntax errors).

I should also point out that if you return false from your error handling function, then the built-in PHP error handler will step in.

You may want this for special logging of any specific kinds of exceptions in your application.

于 2011-06-16T13:39:55.090 回答
3

如果这是正确的,是否有任何设置自定义错误处理程序的目的?我总是保留error_reporting(-1),所以无论如何我都应该在错误日志中得到所有错误,对吧?

error_reporting 指示应该报告哪些错误(例如警告、通知等)。这与它们出现在日志中无关;如果您将 error_reporting 设置为 0,它将不会被记录。如果您希望它被记录,但不向客户显示,请使用 display_errors = Off。

至于用户体验,我看不到以自定义方式显示这些错误的目的——你为什么希望用户看到它们?在生产中,我总是将 display_errors 设置为关闭。

我希望看到“某事”出错的消息,而不是看到“白屏死机”。使用自定义错误处理程序可能很简单。所以,是的,它确实有一个目的。

于 2011-06-16T13:42:44.307 回答