-
Notifications
You must be signed in to change notification settings - Fork 7.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow Throwable to be extended outside of Exception #17233
Comments
While the methods are final, the properties can be trivially set via reflection. (And mostly, the methods just return those.) |
Related to php#17233 Allow `\Throwable` to be extended directly without causing a Fatal error. * **Zend/zend_exceptions.c** - Remove the restriction in `zend_implement_throwable` function that checks if a class implementing `\Throwable` extends `Exception` or `Error`. - Update the `zend_register_default_exception` function to register the `zend_ce_throwable` interface without the restriction. * **Zend/tests/bug_test.phpt** - Add a new test file to ensure that extending `\Throwable` directly works as expected. - Add a test case to check if a class can extend `\Throwable` directly without causing a Fatal error. - Add a test case to check if a class extending `\Throwable` directly can implement the required methods.
From the Throwable RFC: https://wiki.php.net/rfc/throwable-interface
Changing how this works should be discussed in an RFC. |
I do not think implementing What I however support is to remove the |
Description
At present
\Throwable
cannot be directly extended. If you try to it throws a Fatal error:This makes sense for the vast majority of use cases; it prevents people from re-inventing the wheel and trying to implement Throwable when really they should just extend Exception.
However, there are cases where extending Throwable would make sense, particularly in light of the fact that all methods on Exception are final.
For example, tests for exception handling and logging code can't mock Exceptions fully or sensibly. Testing that your log messages are being correctly written to log storage is difficult to make repeatable.
There are 3 solutions to this problem:
I think that option 1 is the simplest. We should rely on the docs to tell people to extend Exception, but allow Throwable to be implemented.
The text was updated successfully, but these errors were encountered: