Hello Juris,
There is some uncertainty for me on how this approach would work with
namespaces.

Let's get this example:
https://3v4l.org/bI1Rj

I would expect to get the error message that I forgot to insert `use
Stringable;`.
But with your idea everything is fine. I don't like such ambiguity and I
worry it may bring some confusion to developers.

Kind regards,
Jorg

On Sat, Dec 28, 2024 at 9:57 PM Juris Evertovskis <ju...@glaive.pro> wrote:

> Hi,
>
>
>
> I would like to propose a new syntax that let’s you implement an interface
> only if it exists.
>
>
>
> `class MyClass extends ?OptionalInterface {}`
>
>
>
> If the `OptionalInterface` exists, the class implements it. If no such
> interface can be found, it is ignored.
>
>
>
> https://wiki.php.net/rfc/optional-interfaces
>
>
>
> The need to declare compatibility with a certain interface without
> requiring the interface itself comes up when you’re writing code that
> should work with various versions of other libraries or PHP itself and when
> a library provides interoperability with some other library or a PHP
> extension, but does not require it.
>
>
>
> Although this would mainly be used by library developers, the consumers of
> those libraries would also benefit as this syntax would clearly communicate
> the intention of optional interoperability. IDEs and static analysis tools
> could also have easier time with `?OptionalInterface` instead of class
> definitions inside an `if (interface_exists(…)) { … } else {…}`.
>
>
>
> I’ve not entirely sure about the naming as an “optional interface” feels
> like an oxymoron. I’ve also considered names like non-required interfaces,
> conditional interfaces, optional/conditional implementation or even soft
> interfaces/soft implementation. Please let me know if the naming itself
> bothers you.
>
>
>
> Previous discussion: https://externals.io/message/125967
>
> WIP implementation: https://github.com/php/php-src/pull/17288
>
>
>
> Looking forward to your feedback and suggestions!
>
> Juris
>

Reply via email to