The problem that Trait Addresses are similar to those that Java addresses with interfaces is how to enforce common behavior (represented by interfaces) among classes that are not part of the same class hierarchy.
With languages like C ++ that only have inheritance, for two objects from two different classes that should be used in the same context, requiring the same behavior, the two classes should have been from the same hierarchy. Sometimes this meant creating completely artificial hierarchies to allow the use of objects from different classes in the same context.
Java solved this problem through interfaces - an interface is essentially a contract governing the provision of behavior, so an object of one class can be replaced with an object of a separate class, because it promises the same behavior - an interface. But they should not be from the same hierarchy.
PHP traits embody this idea. A characteristic is a kind of interface, a set of behavior that a class contains, so that it can be used in a context that requires such behavior. Thus, any example Java interface should be carried over to the example of PHP Traits. PHP traits are slightly different from Java interfaces, though, since Traits can contain complete function definitions, while Java interfaces can only contain declarations (typical PHP features)
source share