Re: Optional interfaces
| From: | Rob Landers | Date: | Fri, 15 Nov 2024 23:39:55 +0000 |
| Subject: | Re: Optional interfaces | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125968@lists.php.net to get a copy of this message | ||
On Sat, Nov 16, 2024, at 00:34, Juris Evertovskis wrote:
> Hey all,
>
> If you try to implement an interface that does not exist, you get the ‘Interface “%s” not
> found’ error. Usually that’s useful as it points to a code error.
>
> However, sometimes we want our code to be compatible with an optional library. For example, my
> Expression class might be compatible with some ORM, but it would also usable without it or with
> another ORM. This can already be accomplished, but the solutions seems very workaroundish:
> https://stackoverflow.com/questions/57996429/implements-interface-only-if-exists
>
> Maybe the PHP itself should allow marking some interfaces as optional — such that they are
> discarded from the list if the interface class does not exist? Here’s one way the syntax could
> look like:
>
> class Expression implements MyInterface, ?OptionalInterface {}
>
> interface EventDispatcherInterface extends ?PsrEventDispatcherInterface {}
>
> BR,
> Juris
Hey Juris,
Is there some reason you cannot do this:
https://3v4l.org/hF1vr
— Rob