note 126619 added to class.errorexception

From: Date: Tue, 16 Nov 2021 15:06:30 +0000
Subject: note 126619 added to class.errorexception
Groups: php.notes 
Request: Send a blank email to php-notes+get-217259@lists.php.net to get a copy of this message
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 */ } ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 50.7.93.28) ---- Manual Page -- https://php.net/manual/en/class.errorexception.php Edit -- https://main.php.net/note/edit/126619 Del: integrated -- https://main.php.net/note/delete/126619/integrated Del: useless -- https://main.php.net/note/delete/126619/useless Del: bad code -- https://main.php.net/note/delete/126619/bad+code Del: spam -- https://main.php.net/note/delete/126619/spam Del: non-english -- https://main.php.net/note/delete/126619/non-english Del: in docs -- https://main.php.net/note/delete/126619/in+docs Del: other reasons-- https://main.php.net/note/delete/126619 Reject -- https://main.php.net/note/reject/126619 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217259) next »