asplode
asplode copied to clipboard
PHP7 implications
PHP7 brings with it significant changes to error handling:
- Most errors are now thrown objects of type
Error. Errorobjects are a separate hierarchy toExceptionobjects.- Both errors, and exceptions implement
Throwable.
As a result, if an application using Asplode wants to handle PHP5 and PHP7 errors simultaneously, it would currently need to do something like this:
try {
// code
} catch (ErrorException $e) {
// handle
} catch (Error $e) {
// handle
}
We could make this simpler, by adding a polyfill for the Error class and Throwable interface. This would allow the application to use just one catch statement:
try {
// code
} catch (Error $e) {
// handle
}
The polyfill would basically do the following:
interface Throwable
{
// appropriate methods
}
class Error extends ErrorException implements Throwable
{
}
Then all of Asplode's error exceptions could inherit from Error.
There are some issues with this idea:
Throwablecannot be directly implemented in PHP7, it may turn out thatErrorcannot be extended either.Errorcan be extended without issue.- When using the polyfill, catching
ErrorExceptionwould also catchError, but this would not be the case under PHP7.