Alternative for $errcontext argument of error handler in php

Viewed 132

I am upgrading php version from 7.0.3 to 7.2.33

I can see that "$errcontext argument of error handler" is deprecated

& i found that on the file in Magento 1 framework uses it like below

 public function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
{
    $errorLevel = error_reporting();

    if ($errorLevel & $errno) {
        
        $this->log($errstr, $priority, array('errno'=>$errno, 'file'=>$errfile, 'line'=>$errline, 'context'=>$errcontext));
    }

    if ($this->_origErrorHandler !== null) {
        return call_user_func($this->_origErrorHandler, $errno, $errstr, $errfile, $errline, $errcontext);
    }
    return false;
}

According to PHP docs :- The $errcontext argument contains all local variables of the error site. Given its rare usage, and the problems it causes with internal optimisations, it has now been deprecated. Instead, a debugger should be used to retrieve information on local variables at the error site.

How do i modify this function, so that it won't throw an warning & work fine ?

Let me know if any questions.

Answers appreciated

1 Answers

Remove $errcontext from your function and consider refactoring $this->log() to eliminate the context key there is a strict requirement for a context parameter:

     public function errorHandler($errno, $errstr, $errfile, $errline)
    {
        $errorLevel = error_reporting();
    
        if ($errorLevel & $errno) {
            
            $this->log($errstr, $priority, array('errno'=>$errno, 'file'=>$errfile, 'line'=>$errline, 'context'=>'Context not supported or deprecated'));
        }
    
        if ($this->_origErrorHandler !== null) {
            return call_user_func($this->_origErrorHandler, $errno, $errstr, $errfile, $errline);
        }
        return false;
    }
Related