for example, I do this:

$var = ss; //неопределённая константа, например file_put_contents('1.log', var_export(error_get_last(), true)); 

As a result, I get null

Real code example:

 function error_handler($errno, $errstr, $errfile, $errline) { //... } function fatal_error_handler() { chdir($_SERVER['DOCUMENT_ROOT'] . '/dsm2.7f2_local/dsm2'); $error = error_get_last(); if ($error && $error['type'] == E_ERROR) { sendErrorMessage('troopermanowar@gmail.com', 'Error', 'Fatal error!'); error_handler($error['type'], $error['message'], $error['file'], $error['line']); } } function sendErrorMessage($email, $subject, $msg) { //... } error_reporting(-1); ini_set('display_errors', 'off'); set_error_handler("error_handler"); register_shutdown_function('fatal_error_handler'); 
  • So there was no error, obviously. An indefinite constant is a note that is converted to a string. Notays are caught differently. - Goncharov Alexander
  • Here is the mistake in the first line - a non-existent constant - Jonny Manowar
  • ok, change to $var[]; , this is Fatal error - Jonny Manowar
  • ok, and the Fatal error is caught in a different way — somehow register_shutdown_function(function(){ if (!($error = error_get_last())){ return;} file_put_contents('1.log', var_export($error, true));}); . Only register_shutdown_function - must be at the beginning of the program. Fatals after such an announcement will fall into kollbek. - Goncharov Alexander
  • I did it initially, but since it did not work, I decided to check that this function returns at all. - Jonny Manowar

1 answer 1

There is generally an interception of any errors except fatal with set_error_handler . Fatal can be intercepted using the register_shutdown_function . There is also set_exception_handler - for a special type of error "uncaught exceptions". Interceptors must be defined at the beginning of the program — for example, with the construction of an object like a Logger.

Well, the exception is manually caught using try{..}catch(Exception $e){/*перехват тут*/} - if not intercepting, it will intercept set_error_handler or set_exception_handler , but it must be remembered that fatals, notes, streaks (types of errors ) - exclusions do not apply, sometimes varnings do not apply.

That's all the error handling in PHP - if you set up a log of all errors and notifications in the log: it helps well, it allows you to produce properly working code.

  • via set_error_handler everything works, but it is also necessary to add fatals to the log as well, but with this the problem arose - Jonny Manowar
  • @Jonny Manowar - for this register_shutdown_function works iron. Only it is necessary to determine it before the fatal - of course) - Goncharov Alexander
  • I define it of course at the beginning of the index.php file - Jonny Manowar
  • @Jonny Manowar This means that interception occurs - but an error during the recording phase. In your code, insert after if ($error && $error['type'] == E_ERROR) lines var_dump(123);die(); - you will see 123, which means that the interception has occurred. And chdir - 100 years have not seen this, write on the absolute path - so it is more correct. That is, do not forget in index.php to declare the path of the site define('PATH', __DIR__) . - Goncharov Alexander
  • var_dump brought nothing - Jonny Manowar