Skip to content

Allow Throwable to be extended outside of Exception #17233

Open
@robincafolla

Description

@robincafolla

Description

At present \Throwable cannot be directly extended. If you try to it throws a Fatal error:

PHP Fatal error: Class FakeException cannot implement interface Throwable, extend Exception or Error instead in /censored/path/to/FakeException.php on line 7

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:

  1. Allow \Throwable to be extended
  2. Make final methods in \Exception non-final to allow stubs to over-ride existing behaviour
  3. Add a new class specifically for tests which implements Throwable and can be extended to stub behaviour

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions