On PHP5 it makes a whole lot of sense having both set_exception_handler()
and set_error_handler()
defined.
However, on PHP7 all (most?) errors are now exceptions. So, what’s the point on defining both handlers, if even errors would pass by the exception handler instead?
I see there’s a note on PHP7 new Error
class in the exception handler doc, but there’s no reference to the fact there’s no plain errors anymore, but Throwable
s, in the error handler function.
Since PHP 7, most errors are reported by throwing
Error
exceptions, which will be caught by the handler as well. BothError
andException
implements theThrowable
interface. [source]
Advertisement
Answer
Aaron Piotrowski (the guy who made the new Error-Exception system) has a great blog on this. I think the key point you need to understand is this
In PHP 7, an exception will be thrown when a fatal and recoverable error (
E_ERROR
andE_RECOVERABLE_ERROR
) occurs, rather than halting script execution. Fatal errors still exist for certain conditions, such as running out of memory, and still behave as before by immediately halting script execution. An uncaught exception will also continue to be a fatal error in PHP 7. This means if an exception thrown from an error that was fatal in PHP 5.x goes uncaught, it will still be a fatal error in PHP 7.Note that other types of errors such as warnings and notices remain unchanged in PHP 7. Only fatal and recoverable errors throw exceptions.
To put this a different way consider this
set_exception_handler()
– Function to handleException
s by default (as of PHP 7.0 this can handle allThrowable
s, so it can catch recoverable errors)set_error_handler()
– Function to handle recoverable errors
In other words, their functionality didn’t change. Anything that triggers them in PHP5 will trigger them in PHP7, it’s just that, now, you can use a try-catch
block at the script level to handle a specific error.