What happens to set_error_handler () in PHP7 now that all errors are exceptions?

In PHP5, it makes a lot of sense by defining both set_exception_handler() and set_error_handler() .

However, in PHP7 all (most?) Errors are now exceptions. So, what is the definition point for both handlers, even if errors were thrown by the exception handler?

I see in the new Error PHP7 class a new Error class, but there is no reference to the fact that there are no simple errors, but Throwable s, in the error handler function.

Starting with PHP 7, most errors are reported, throwing Error exceptions, which will also be caught by the handler. Both Error and Exception implement the Throwable interface. [source]

+6
source share
2 answers

Aaron Piotrowski (the guy who created the new Error-Exception system) has a great blog on this . I think the key point you need to understand is

In PHP 7, an exception will be thrown when a fatal and recoverable error ( E_ERROR and E_RECOVERABLE_ERROR ) E_RECOVERABLE_ERROR , rather than stopping the execution of the script. Fatal errors still exist for certain conditions, such as a lack of memory, and still behave as before, immediately terminating the script. An uncaught exception will also remain a fatal error in PHP 7. This means that if an exception arising from an error that was fatal in PHP 5.x is not implemented, it will still be a fatal error in PHP 7.

Note that other types of errors, such as warnings and notifications, remain unchanged in PHP 7. Only fatal and recoverable errors raise exceptions.

To say this, consider this

  • set_exception_handler() is the function to handle Exception by default (since PHP 7.0 this can handle all Throwable s, so it can catch recoverable errors)
  • set_error_handler() - Function for handling recoverable errors

In other words, their functionality has not changed. Everything that runs them in PHP5 will call them in PHP7, it's simple, now you can use the try-catch at the script level to handle a specific error.

+4
source

http://php.net/manual/en/language.errors.php7.php reads well about this:

PHP 7 changes how most errors are reported by PHP. Instead of reporting errors through the traditional error reporting mechanism used by PHP 5, most errors are now reported by throwing error exceptions.

As with normal exceptions, these errors are eliminated until they reach the first matching catch block. If there are no corresponding blocks, then any default exception handler set using set_exception_handler () will be called, and if there is no default exception handler, then the exception will be converted to a fatal error and will be treated as a traditional error.

This means that errors are not technically exceptions, but they can be caught as exceptions (which is a nice feature).

For example, the following should work as before:

  set_error_handler('handleError'); try { // raise error } catch (Exception $e) { // won't catch error } 

However, the following is also possible:

  try { // raise error } catch (Exception $e) { // won't catch error } catch (Error $e) { handleError(); } 
+3
source

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


All Articles