Catching PHP errors can be done through two different methods, depending on which error one wants to catch:
Setting en error handler is done through the set_error_handler()
function:
mixed set_error_handler (
callable $error_handler
[, int $error_types = E_ALL | E_STRICT ]
)
The following error types cannot be handled with a user defined function: E_ERROR
, E_PARSE
, E_CORE_ERROR
, E_CORE_WARNING
, E_COMPILE_ERROR
, E_COMPILE_WARNING
, and most of E_STRICT
raised in the file where set_error_handler()
is called.
Note: An error caught through an error handler could potentially be turned into a different error caught by the shutdown handler.
Note2: An error caught through an error handler could potentially be turned into an ErrorException
caught by the exception handler.
Setting a shutdown handler is done through the register_shutdown_function()
function:
void register_shutdown_function (
callable $callback
[, mixed $parameter ]
[, mixed $... ]
)
Multiple shutdown handlers can be registered, and they will be processed in the order they were registered.
The shutdown handler becomes active when PHP is closing its process, either because the error state it encountered is not recoverable anymore, or because the normal execution has come to an end. This means that any extra behaviour we add to shutdown handlers needs to bail early in case of regular process termination.
Looking at the list of possible PHP errors lets us exclude the following categories:
This leaves us then with the following errors we want to catch: