I describe a class - an error handler. Some of his methods are:

public function __construct() { if (DEBUG) { error_reporting(-1); ini_set('display_errors', true); } else { error_reporting(0); ini_set('display_errors', false); } set_error_handler([$this, 'errorHandler']); set_exception_handler([$this, 'exceptionHandler']); register_shutdown_function([$this, 'fatalErrorHandler']); } public function errorHandler($code, $message, $file, $line) { echo '<br />' . __METHOD__ . '<br />'; // some code die; } public function fatalErrorHandler() { echo '<br />' . __METHOD__ . '<br />'; // some code die; } public function exceptionHandler(\Throwable $e) { echo '<br />' . __METHOD__ . '<br />'; // some code die; } 

The logic is as follows: at the beginning of the application, an instance of this object is created and stored in the static property of the main object of the application, which lives all the time. As you can see, error handlers and exceptions are initialized in the object's constructor.

The problem is this: fatal errors, such as Parse error, are not caught by my handler. What is the problem?

  • it seems to me that you, in all your desire, in principle, do not catch the parse error . - teran
  • @Other file parsing errors occur before the script is executed in principle. - teran
  • @teran, the author of one of the answers, uses php.ini , which includes the code with registering the handler before executing the scripts themselves. - user207618
  • one
    @teran, well, this is another question, this one pays attention to this problem. Although, for a virgin, this is a good idea - beautifully designed errors, even of this level. - user207618

1 answer 1

look at the documentation at least sometimes

Errors of the following types cannot be processed by the user: E_ERROR , E_PARSE , E_CORE_ERROR , E_CORE_WARNING , E_COMPILE_ERROR , E_COMPILE_WARNING , and most E_STRICT errors that occurred in the file where the set_error_handler() function is set_error_handler() .

  • it says "set an exception handler with set_exception_handler ()". I assumed that I could catch this error in exceptionHandler'e, or in fatalErrorHandler'e after the completion of the script, but it does not work. Maybe for this. - rugleb
  • @rugleb, pay attention to this question . - user207618