set_exception_handler() Must Update Its Type To Throwable¶
Until PHP 7.0, all thrown issues were children of the Exception
class. In PHP 7.0, all issues are children of Throwable
. Exception
is not only one of two classes implementing it, along with Error
.
To keep compabitility, it is important to switch types.
PHP code¶
<?php
// PHP 5.6- typed with Exception
class foo {
static function bar(\Exception $e) {
print $e->getMessage();
}
}
set_exception_handler([Foo::class, 'bar']);
// Produces an error
1 / 0;
?>
Before¶
PHP Warning: Division by zero in /codes/setExceptionHandlerType.php on line 13
Warning: Division by zero in /codes/setExceptionHandlerType.php on line 13
After¶
PHP Fatal error: Uncaught TypeError: foo::bar(): Argument #1 ($e) must be of type Exception, DivisionByZeroError given in /codes/setExceptionHandlerType.php:5
Stack trace:
#0 [internal function]: foo::bar(Object(DivisionByZeroError))
#1 {main}
thrown in /codes/setExceptionHandlerType.php on line 5
PHP version change¶
This behavior changed in 8.0