phperror-handling

Upgrading from PHP7.4 => PHP8, is it possible to ignore certain errors


We have a huge codebase where we ignored the notice from php7 when accessing undefined variables or array-keys. For example

$somethingThatMayNotExist = $_REQUEST['somethingThatMayNotExist']

PHP8 now throws an Error. I know that we should always check if the key exists, or if the variable is defined. But the codebase is so big and we have poor unit-test coverage that I would rather switch to php8 and have these errors handled as notices (And setting the value to null). Is this with somekind of custom error-handler possible? If so, how to do that?


Solution

  • Based on @KikoSoftware'ss comment I solved this issue by creating a custom error-handler and ignored these types of error by logging and consolidating them to be refactored.

    set_error_handler(array($this,'handleError'))
    
    public function handleError($code, $message, $file, $line)
    {
         if(str_contains($message, 'whatever errormessage should be ignored') {
                // TODO: send mail or log error somewhere for further refactoring
                return null; // php7.4-like behaviour
          }
           
          parent::handleError($code, $message, $file, $line);
    }