PHP 8.5.0 Beta 1 available for testing

Voting

: min(zero, four)?
(Example: nine)

The Note You're Voting On

Christopher Marshall
3 years ago
Going on further from the point made by triplepoint at gmail dot com:

I'm using PHP 7.4.0 and trying to introduce error handling, exception handling and fatal exception handling into my application. A lot of the info all over the internet is now out of date in regards to handling errors with the new changes in PHP 7 and 8, which makes it difficult at the best of times to understand everything.

However what I've found is that by using register_shutdown_function to handle fatal exceptions, it works as expected. set_exception_handler also works perfectly in conjunction. The issue comes when you use set_error_handler as well, and you trigger a custom error (for example using trigger_error) - even if you're using E_ERROR or E_USER_ERROR.

This is because PHP is trying to handle the error before it shuts down and before the register_shutdown_function is actually involved. So it's very important to be mindful of this if you're using different methods for exceptions, errors and fatal exceptions. You will need to specifically catch the error like before and return out of your error handling function for the fatal exception handler to kick in properly.

You're welcome .....

<?php
/**
* We handle basic errors differently to everything else
*/
public static function errorHandler($errStatus, $errMsg = 'Unknown error', $errFile = 'unknown', $errLine = 0)
{
/**
* Because we're using set_error_handler, PHP tries to be
* clever and routes fatal errors and other "errors"
* (i.e. trigger_error) here before it goes to
* register_shutdown_function, so we need to be sure these
* are caught and dealt with in the correct way
*
* @See https://www.php.net/manual/en/class.errorexception.php#95415
*/
if (\in_array($errStatus, [E_ERROR, E_PARSE, E_CORE_ERROR, E_USER_ERROR, E_ERROR]))
{
return;
}

/* Handle everything else however you want */
}

<< Back to user notes page

To Top