On Tue, Feb 21, 2023 at 3:30 PM someniatko <somenia...@gmail.com> wrote:

> > This sounds interesting but it breaks some expectations.
> >
> > Interesting because you can have any class act as an interface for other
> classes with the interface being built up of any public properties or
> method that exists on that class.
>
> In my original suggestion, it's not like any class, but only those you
> specifically mark as `class interface`, not just `class`. Closer to
> the end of my idea I also suggest that maybe it's worth it to allow
> implementing, like interfaces, ANY classes, not only those
> specifically marked with the new syntax, but this indeed brings up
> some issues.
>
> > The expectation it breaks is that if you have a final class, whenever
> you use it, you expect some specific implementation exists on it.
> > And this would break the expectation and in real life someone might pass
> a totally different implementation.
> > Will not break generic LSP but still will break some expectations.
>
> That's why a new syntax is brought up. This expectation might indeed
> break if we allow to reimplement any existing final class, instead of
> requiring a manual opt-in.
>
>
Understood, thanks for explaining.
So it's mostly a syntactic sugar that allows defining an interface for the
current class that is already implemented by the current class.

The interface name is the same as the class name and based on the context,
you either use one or the other.
There are some places where this could get in conflict like (new
ReflectionClass(A::class))->isInterface(). But also internally things might
be difficult to sort out to know what to use one or the other.

To have a perfect syntax-sugar, why not also allow the interface name to be
specified?
Something like:
final class interface(ServiceInterface) Service {
    public function method1() {}
    protected function method2() {}
}

that would generate:
interface ServiceInterface {
    public function method1();
}
final class Service implements ServiceInterface {
    public function method1() {}
    protected function method2() {}
}

As explained previously (and now I got that), autoloading will not work in
this case. But some classmaps can be configured for composer or you could
just force class loading with class_exists() in limited places where this
is needed (in tests).

Alex

Reply via email to