I've found a lot of attempts at all-inclusive error handling implementations, and I figured I might write up a wiki-style to hopefully provide a complete solution that I came up with.
The question is:
"How might one catch, handle, or intercept ALL error types in PHP?"
Now - this might be deemed a 'rewrite' by some - but I don't know that there's been a comprehensive solution proposed.
Into file [Config.php] in [phpMyAdmin-4.6.5.2-all-languages] you can find:
and just in end of file:
There are three kinds of error handlers you need:
set_exception_handler
, to catch any otherwise uncaught exceptions.set_error_handler
to catch "standard" PHP errors. I like to first check it againsterror_reporting
to see if it's an error that should be handled or ignored (I generally ignore Notices - probably bad but that's my choice), and throw anErrorException
, letting the exception handler catch it for outputting.register_shutdown_function
combined witherror_get_last
. Check the value of['type']
to see if it isE_ERROR
,E_PARSE
or basically any fatal error types you want to catch. These normally bypassset_error_handler
, so catching them here lets you grab them. Again, I tend to just throw anErrorException
, so that all errors end up being handled by the same exception handler.As for how you implement these, it depends entirely on how your code is set up. I usually do an
ob_end_clean()
to purge any output, and present a nice error page telling them that the error has been reported.There are a number of levels of PHP errors, some of these require setting separate error handlers, and in order to catch every error PHP might throw up - you must write something that encompasses all of these 'types' of errors -- startup, 'runtime', and exceptions.
My solution to catch every ( as far as I can tell ) error that comes down the pipe:
...
Now - code out of the way...
To implement this, it's as simple as including this file, and executing the 'InitializeErrors' method. Beyond this, it's up to you what you want to Do with the errors; this is simply a wrapper for every error that PHP might generate - and to make changes to how any given error is handled, it's basically as simple as evaluating it in the 'AppendError' method and responding accordingly.
-- I should note - I implemented return values for reasons I cannot explain, and I should review my own work on that front - but I'm not terribly sure it has any bearing on the result.