PHP error management

I searched the net to try and find a way to catch all the errors thrown by PHP (5.3)

I read the documentation and it seems that set_error_handler is what I need, but it does not lead to fatal / parsing errors. I'm not sure if this is possible ...

Here is my source: https://github.com/tarnfeld/PHP-Error-Handler Feel free to fork / commit if you know the best solutions for all this.

Thanks, advanced!

Updated!

Using the answers below, I ended up writing an error handler, it takes care of E_ERROR | E_PARSE | E_WARNING | E_NOTICE and kill the script when it is deadly !:-)

+1
source share
5 answers

Guideline Note:

The following types of errors cannot be handled by a user-defined function: E_ERROR , E_PARSE , E_CORE_ERROR , E_CORE_WARNING , E_COMPILE_ERROR , E_COMPILE_WARNING and most E_STRICT raised in the file where set_error_handler() .

This means that Fatals cannot be caught and processed. What you can do is set up an extra handler that will run when the script exits, for example. register_shutdown_function .

In this handler, use error_get_last and check if it was Fatal Error. This will not allow you to continue executing the script. the script will end, but you can do any cleanup or logging (usually fans will write to error.log anyway) or something else.

In the comments below set_error_handler

there is an example given by the user.
 register_shutdown_function('shutdownFunction'); function shutDownFunction() { $error = error_get_last(); if ($error['type'] == 1) { //do your stuff } } 

But keep in mind that this will still catch certain additional runtime errors.

+4
source

You can follow error_log . On Ubuntu, this is in /var/log/apache2/error_log

+2
source

The so-called "fatal errors" - this is perhaps the most unpleasant mistake in php. The best we can do is convince the php group to recognize this error as such by voting for this request http://bugs.php.net/bug.php?id=28331 .

Until this is fixed, we are doomed to use dirty hacks, such as shutdown or startup error handlers.

+2
source

No, catching fatal and syntax errors is impossible. Parsing errors because they are raised before the script is even compiled. Fatal errors because they are ... fatal (i.e. the script cannot continue to work after they are detected)

0
source

If you want to catch fatal errors (including parsing errors), the best solution is to set error_logging in the php.ini file, for example.

 display_errors = Off log_errors = On ignore_repeated_errors = Off ignore_repeated_source = Off 
0
source

Source: https://habr.com/ru/post/1383851/


All Articles