On Sat, Dec 28, 2024, 22:58 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
>
>
> Hi Juris,

I think the proposal looks good but I some some points to be discussed:

>From what I understand, the autoloading mechanism would be triggered for
the optional interface, but it would not be an error if it would not
succeed. I think we should explicitly spell this out in the RFC, to make it
clear that autoloading will be attempted.

If an interface was not available at class declaration time, the function
class_implements (on both class and object) will return false going
forward, as if the class definition did not contain the interface at all?

Similarly, if at runtime the object would be passed to a function/method
that has the parameter type of that interface, would it fail saying that
the object is not an instance of that interface?
Note: autoloading is not triggered when just using an interface/class name
in a parameter type or as return type.

I think completely erasing the interface sounds good, I just think it
should be explicitly spelled out in the RFC, and all the implications that
follows.


If a second class will implement the same optional interface, will it
trigger autoloading again? I think it should.

If in the meantime, between defining two such classes, the interface would
be defined (using a hack or some sort), would it cause problems? I think
that given the current existing workarounds this is a valid concern.
To avoid problems, it would be wise to completely erase the interface from
first class definition when it was not available, even if the second class
might have it.

-- Alex

Reply via email to