On Sat, Nov 16, 2024, at 01:27, Marco Aurélio Deleu wrote:
>
>> 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?
Thanks Marco,
I don't visit stackoverflow because I loath "features locked behind internet points". Thanks for letting me know what is behind the 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.
I maintain a PHP SDK that needs to be compatible with Symfony and Laravel and any other framework. The shenanigans are all bound in one file that I very rarely touch. So, I do kinda like the idea of having optional interfaces, but I feel like it is rather niche. As an application developer, I can't think of one time I've ever needed this.
However, the single biggest advantage comes from IDEs that can somehow show that there is an optional interface on a type. I think that would be the most useful bit to come from this, honestly. It also prevents IDEs or static analysis from giving a warning that there are multiple definitions of an interface if you have the appropriate libraries installed.
It's also rather simple to implement, so I think it is worth pursuing, IMHO.
— Rob