Re: [RFC] Traits with interfaces
| From: | Levi Morrison | Date: | Wed, 17 Feb 2016 23:13:24 +0000 |
| Subject: | Re: [RFC] Traits with interfaces | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-91261@lists.php.net to get a copy of this message | ||
On Wed, Feb 17, 2016 at 12:48 PM, Kevin Gessner <kgessner@etsy.com> wrote:
> On Wed, Feb 17, 2016 at 10:26 AM, Levi Morrison <levim@php.net> wrote:
>>
>> I want to add my personal experience with traits: every time I create
>> a trait it is to implement an interface. Here is a publicly available
>> [example with
>>
>> OuterIterator](https://github.com/morrisonlevi/Ardent/blob/master/src/Collection/OuterIteratorTrait.php):
>>
>> [snip]
>>
>> I can see how it would be nice to allow the trait to officially
>> declare that it implements some interface (in this case Iterator or
>> OuterIterator) which would require that the trait has fully
>> implemented the required methods (or declared them as abstract). This
>> would be a small improvement but helpful.
>>
>> I am less certain about the classes which
use it
>> automatically
>> inheriting the interfaces. To clarify: I am neither in favor or
>> against that part. Or at least at this stage, anyway.
>
>
> I don't think there is enough benefit from allowing traits to declare
> interfaces, but not propagating the interface out to classes that insert the
> trait. It does provide an extra bit of enforcement on the trait's methods,
> but the real value is in making that contract a part of the classes.
I do agree that there is only a little benefit but I disagree that it
is "not enough". It's a quality of life change to an existing feature
that has no impact outside of that feature – sounds perfect to me.
> In what situation would a class that inserts OuterIterator not want to
> declare the Iterator class? If every class that inserts the trait would
> also declare Iterator, it's needless repetition to require that both the
> class and the interface.
Funny you should ask, because OuterIteratorTrait implementing
OuterIterator is actually a case where not all using classes will want
to be OuterIterators formally (just get the horizontal code reuse).
This is because I don't want to "leak" the inner iterator because it
is suppose to be abstracted. In these cases I use the syntax for using
a trait method under a different visibility.