Re: Traits with interfaces
| From: | Marco Pivetta | Date: | Thu, 01 Mar 2018 00:34:31 +0000 |
| Subject: | Re: Traits with interfaces | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-101942@lists.php.net to get a copy of this message | ||
This cannot work for a number of reasons:
- a trait is not a type, and does not practically exist at runtime
- trait defined API can be imported with changed/aliased names, breaking
therefore the contact defined in the interface
- due to the previous point, inheriting a type from a trait becomes a
dangerous BC boundary, easily broken by consumers of the trait when
consumers alias or when the trait implementor adds a new interface
implementation
My general suggestions:
- don't use traits
- don't use traits
- also, don't use traits
- remember to not use traits
- traits: don't
- things you shouldn't use on Betelgeuse and other systems: traits
Besides jokes, inheriting signatures together with implementations
(inheritance, abstract types) is less and less endorsed in the PHP
ecosystem, as it just increases coupling by a huge lot. At least from my
own experience, things are finally moving towards more composition over
inheritance.
On 28 Feb 2018 21:58, "David Rodrigues" <david.proweb@gmail.com> wrote:
> Why traits doesn't supports interfaces (via implements) like classes does?
>
> It could be useful when trait implements part of abstract functions from
> interface, then the class that uses this traits should implements the
> another part.
>
> It could turn it possible (pseudo-code):
>
> interface VisibilityControlContract
> - public function isVisible(): bool;
>
> trait VisibilityControlTrait implements VisibilityControlContract
> - public function isVisible(): bool { ... }
>
> class UserModel (not need implements VisibilityControlContract directly)
> - uses VisibilityControlTrait;
>
> var_dump(new UserModel instanceof VisibilityControlContract); // true
>
> The disvantages that I can see with that is that without an IDE I could not
> identify easily if interface was implemented by some trait. But it too
> happen when I do implements an abstract class that implements some
> interface.
>
> --
> David Rodrigues
>