On Wed, Feb 17, 2016 at 9:34 AM, Sebastian Bergmann <sebast...@php.net>
wrote:

> Am 17.02.2016 um 15:25 schrieb Kevin Gessner:
> > Hello internals team!  I'd like to propose an RFC to allow traits to
> > implement interfaces.
>
>  I think that would violate "The Flattening Property" [1], meaning
>  that the fact that a class uses a trait must not be noticable by a
>  user of that class.
>

I don't believe that it will violate that property.  Take this code for
example, which would be valid with the proposed change:

<?php

interface I {
    function foo();
}

trait T implements I {
    function foo() { }
}

class C {
    use T;
}

print_r(class_implements(C::class));
// Array
// (
//     [I => I]
// )


Given only class C, an outside caller would not be able to distinguish it
from a class that implements I directly, or from a class that implements I
via inheritance.  Is there an additional wrinkle here that I've not
considered?


>  Also bear in mind that explicit is better than implicit. Two
>  explicit declarations, "implements <interface>" and "uses <trait>",
>  makes it obvious from looking at the code of a class that is
>  provides a specific API and uses a trait for its implementation.
>

I consider this sort of implicit declaration a benefit, in the spirit of
DRY.   It's in the same vein that an interface can be implicitly
implemented by way of a class's superclass, which reduces code duplication
and extra declarations in subclasses.

And in fact it makes the trait itself more explicit: if the intention of
the code is that a trait provides an implementation of a particular
interface (I've found several in-the-wild examples of this, such as
[1][2]), all the better that the trait explicitly declares that interface.

1:
https://github.com/symfony/symfony/blob/582f4753a343f230fbe18b4e9a0747d48351ddfb/src/Symfony/Component/DependencyInjection/ContainerAwareInterface.php
2:
https://github.com/symfony/symfony/blob/582f4753a343f230fbe18b4e9a0747d48351ddfb/src/Symfony/Component/DependencyInjection/ContainerAwareTrait.php


>
>  --
>  [1] https://wiki.php.net/rfc/horizontalreuse
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Reply via email to