Re: Optional interfaces
| From: | Rob Landers | Date: | Sat, 16 Nov 2024 14:35:44 +0000 |
| Subject: | Re: Optional interfaces | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-125974@lists.php.net to get a copy of this message | ||
On Sat, Nov 16, 2024, at 12:43, Valentin Udaltsov wrote:
> On Sat, 16 November 2024 at 13:42 Rob Landers <rob@bottled.codes> wrote:
>> __
>> On Sat, Nov 16, 2024, at 10:02, Juris Evertovskis wrote:
>>> Is there some reason you cannot do this:
>>>
>>> https://3v4l.org/hF1vr
>>>
>>> Unless I’m missing something, you can’t patch-define a namespaced OptionalInterface
>>> that easily.
>>>
>>> Anyway, I was not implying this is something impossible to accomplish. My point is that
>>> it would be beneficial to have syntax for expressing “if your app has this interface, our class
>>> implements it”.
>>>
>>> Juris
>>
>> Yeah, I get it now (see my reply to Marco).
>>
>> Even with namespaces, it is pretty straightforward, though:
>> https://3v4l.org/dmXoB
>>
>> — Rob
>
> This is a nice solution.
>
> However, interface_exists should be used for interfaces.
>
> --
> Valentin
>>
Actually, you'd probably want to check for both (as well as traits) for a better developer
experience.
For example, using interface_exists results in the error "Cannot declare interface, because the
name is already in use" if a class with the same name is used instead of an interface. This is
a nonsensical error message because the library is not responsible for declaring it.
Instead, you'd want the error message to be more actionable: "cannot implement class -- it
is not an interface"
— Rob