Re: Optional interfaces
| From: | Marco Aurélio Deleu | Date: | Sat, 16 Nov 2024 00:27:14 +0000 |
| Subject: | Re: Optional interfaces | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125969@lists.php.net to get a copy of this message | ||
> On 15 Nov 2024, at 20:42, Rob Landers <rob@bottled.codes> wrote:
>
>
>> 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
That seems to match what he shared on SO link?
I think I opened this email with a strong negative feeling towards what would be optional interface,
but to be honest a syntax sugar where implements ?MyInterface becomes something pretty close to what
Rob shared doesn’t sound bad, it’s just convenience. Unfortunately PHP is not keen on being open
to those.
Overall, I dig it.