Re: RFC: Trait expects interface
| From: | David Gebler | Date: | Wed, 05 Jan 2022 19:16:58 +0000 |
| Subject: | Re: RFC: Trait expects interface | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-116819@lists.php.net to get a copy of this message | ||
On Tue, Jan 4, 2022 at 10:35 PM Kirill Nesmeyanov <nesk@xakep.ru> wrote:
> How relevant do you think this idea/proposal is? And what possible
> problems or solutions will this entail in the future?
>
I'm not convinced there's a reasonable need for it. The very nature of
finding yourself in a situation where you want any class using a trait to
also require other things outside the trait kind of suggests you really
want to be using interfaces or abstract classes anyway.
There is a similar concept for what I think you're trying to achieve in
Java, though, which could also be useful in PHP if it was feasible within
the engine - the ability to provide a default method implementation on
interfaces themselves.
Short of that, we can already effectively get there with the tools we have;
an abstract class can use a trait and define abstract or concrete methods,
and in doing so can implement one or more interfaces. Obviously traits can
also declare abstract methods but I assume it's the identity/type aspect of
an interface you want here which isn't satisfied by that approach.
Also worth noting, although I can't say I'm familiar with the mechanics of
traits in PHP's implementation, my understanding has always been that
they're effectively compiler-level copy and paste in to a class so I'm not
sure what interfaces are implemented by the class (or conversely allowing a
trait to implement an interface) would be readily achievable.
That's my two cents, good luck with whatever you're trying to do anyway.
- David
> --
> Kirill Nesmeyanov