I write my class error handler, in the constructor I implement the following handlers:

set_error_handler([$this, 'errorHandler']); set_exception_handler([$this, 'exceptionHandler']); ob_start(); register_shutdown_function([$this, 'fatalErrorHandler']); 

In each of these methods, I throw a new exception, for example:

 public function fatalErrorHandler() { $error = error_get_last(); if ( !empty($error) && $this->isFatalError($error['type']) ) { ob_get_clean(); throw new ErrorException($error['message'], $error['type']); } else { ob_end_flush(); } } 

The ErrorException class is the grandson of the base \ Exception:

 class ErrorException extends Exception { const RESPONSE_CODE = 500; } class Exception extends \Exception { /* +новые функции */ } 

Problem: when testing a fatal error handler, for example, when calling a non-existent function, a fatal error crashes:

 Fatal error: Uncaught lynx\core\handlers\ErrorException: Uncaught Error: Call to undefined method **Error::array()** in vendor\lynx\core\ErrorHandler.php:124 Stack trace: #0 [internal function]: lynx\core\ErrorHandler->exceptionHandler(Object(Error)) #1 {main} thrown in C:\OpenServer\domains\smart-as.dev\vendor\lynx\core\ErrorHandler.php on line 110 

That is, an Error object that I threw out is not thrown to the exception handler, but an Error object. It is clear that he is also a descendant of Throwable, but why does he come then?

If you throw an exception of the global \ Exception object, then it is the same - the same Error comes in the exeptionHandler (code below). I can not understand what the problem is, I do not understand something, but I can not google it.

 public function exceptionHandler(Exception $e) { $this->error = $e->array(); $this->displayError($e->getResponseCode()); $this->logErrors(); } 

Thank you in advance!

  • php 7 apparently you (see the list of parameters, the second paragraph)? - teran
  • @teran is absolutely true - rugleb

2 answers 2

According to the documentation:

set_exception_handler - Sets a custom exception handler

Parameter List: $ exception_handler

The name of the function to be called each time an uncaught exception is thrown. A handler function must take one argument — an object representing the thrown exception. Before PHP 7, this function looks like this:

 void handler ( Exception $ex ) 

In PHP 7, most internal errors now generate an exception, but of the Error class. For them, the specified exception handler will also be called. The Error and Exception classes implement the Throwable interface, so it should be used in the handler function signature:

 void handler ( Throwable $ex ) 

Warning
Specifying the Exception type for the ex parameter in your handler function will cause problems in PHP 7 due to the changed hierarchy of exception classes.

In general, in your case, everything, as written in the docks, one to one.

  • Understood, the problem seems to be here: В PHP 7 большинство внутренних ошибок теперь генерируют исключение, но класса Error. It turns out that for such errors I cannot throw a descendant of even the class \ Error? - rugleb
  • @rugleb as I understand it, you can throw out anything, you just have to work with it through Throwable , and then continue to check the real type and, if necessary, bring it explicitly. - teran

From the documentation :

In PHP 7, most internal errors now generate an exception, but of the Error class. For them, the specified exception handler will also be called. The Error and Exception classes implement the Throwable interface, so it should be used in the handler function signature.

Accordingly, in my situation, in principle, first the exceptionHandler worked, in which the Error object arrived. And I thought it was marveled by the fatalErrorHandler .