Re: [RFC] Interface Default Methods
| From: | Alexandru Pătrănescu | Date: | Tue, 20 Jun 2023 05:21:50 +0000 |
| Subject: | Re: [RFC] Interface Default Methods | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-120634@lists.php.net to get a copy of this message | ||
On Tue, Jun 20, 2023 at 7:30 AM Levi Morrison <levim@php.net> wrote:
> On Sat, Jun 17, 2023 at 6:05 AM Alexandru Pătrănescu <drealecs@gmail.com>
> wrote:
> >
> > 1. Do we want to allow also private methods in the interface so that it
> > allows code reuse or better code organization in smaller methods?
>
> Sorry for the delay in responding to your message. I have implemented
> support for private methods for people to experiment with. As far as I
> can tell, it works as expected and I don't see any issues with them.
> Of course, they only make sense as helper methods to the public
> default methods.
>
> I'd like to hear what others think about allowing private interface
> methods that have method bodies, but I think it's easy and sensible.
>
Looks good to me. Thank you!
>
> > Would a method implemented by a trait have higher precedence over the
> > interface default implementation?
> > Would a trait offered implementation be directly usable by the interface?
>
> I'm not entirely sure what you are asking about traits, so I'll try to
> clarify. In a class which uses a trait to implement an interface, that
> trait method will take priority over the interface's default method. I
> will add a test to the PR to make this more obvious.
>
Yes, that looks good, this was the first question, exactly.
Sorry for the lack of clarity in my second question.
Is it if it would be allowed to have something like:
trait Trait1 {
function method1() { echo __METHOD__, "\n"; }
}
interface Interface1 {
use Trait1;
function method1();
}
So both trait and interface can be used independently without code
duplication.
A more complex scenario where this might be useful:
// external package
interface Interface1 {
function method1();
}
interface Interface2 {
function method2();
}
// internal package
trait Trait1 {
function method1() { echo __METHOD__, "\n"; }
}
trait Trait2 {
function method2() { echo __METHOD__, "\n"; }
}
interface Interface3 extends Interface1, Interface2 {
use Trait1;
use Trait2;
}
Where it will allow either usage of a class for:
- Interface1 with Trait1
- Interface2 with Trait2
- Interface3 that would already have the method implemented.
Hope it's clearer now.
I don't know if we want to support use of a trait in the interface
but I think the RFC should mention that it is or is not supported.
Thank you,
Alex