Search code examples
phperror-handlingcustom-error-handling

PHP - should set_error_handler be used?


From my understanding of PHP documentation, setting custom error handler (which cannot catch all the errors) deactivates the default php error handler (which I guess can catch all the errors:).

If this is correct, is there any purpose of setting custom error handler? I always keep error_reporting(-1), so I should get all errors in the error log anyway, right?

As for the user experience, I cannot see the purpose of making these errors manifest in a custom way - why would you ever want users to see them? In production I always set display_errors to off.


Solution

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