Traits
As of PHP 5.4.0, PHP implements a method of code reuse called Traits.
Traits are a mechanism for code reuse in single inheritance languages such as
PHP. A Trait is intended to reduce some limitations of single inheritance by
enabling a developer to reuse sets of methods freely in several independent
classes living in different class hierarchies. The semantics of the combination
of Traits and classes is defined in a way which reduces complexity, and avoids
the typical problems associated with multiple inheritance and Mixins.
A Trait is similar to a class, but only intended to group functionality in a
fine-grained and consistent way. It is not possible to instantiate a Trait on
its own. It is an addition to traditional inheritance and enables horizontal
composition of behavior; that is, the application of class members without
requiring inheritance.
Trait example
]]>
Precedence
An inherited member from a base class is overridden by a member inserted
by a Trait. The precedence order is that members from the current class
override Trait methods, which in turn override inherited methods.
Precedence Order Example
An inherited method from a base class is overridden by the
method inserted into MyHelloWorld from the SayWorld Trait. The behavior is
the same for methods defined in the MyHelloWorld class. The precedence order
is that methods from the current class override Trait methods, which in
turn override methods from the base class.
sayHello();
?>
]]>
&example.outputs;
Alternate Precedence Order Example
sayHello();
?>
]]>
&example.outputs;
Multiple Traits
Multiple Traits can be inserted into a class by listing them in the use
statement, separated by commas.
Multiple Traits Usage
sayHello();
$o->sayWorld();
$o->sayExclamationMark();
?>
]]>
&example.outputs;
Conflict Resolution
If two Traits insert a method with the same name, a fatal error is produced,
if the conflict is not explicitly resolved.
To resolve naming conflicts between Traits used in the same class,
the insteadof operator needs to be used to choose exactly
one of the conflicting methods.
Since this only allows one to exclude methods, the as
operator can be used to add an alias to one of the methods. Note the
as operator does not rename the method and it does not
affect any other method either.
Conflict Resolution
In this example, Talker uses the traits A and B.
Since A and B have conflicting methods, it defines to use
the variant of smallTalk from trait B, and the variant of bigTalk from
trait A.
The Aliased_Talker makes use of the as operator
to be able to use B's bigTalk implementation under an additional alias
talk.
]]>
Prior to PHP 7.0, defining a property in a class with the same name as in a trait
would throw an E_STRICT if the class definition was compatible
(same visibility and initial value).
Changing Method Visibility
Using the as syntax, one can also adjust the visibility
of the method in the exhibiting class.
Changing Method Visibility
]]>
Traits Composed from Traits
Just as classes can make use of traits, so can other traits. By using one
or more traits in a trait definition, it can be composed partially or
entirely of the members defined in those other traits.
Traits Composed from Traits
sayHello();
$o->sayWorld();
?>
]]>
&example.outputs;
Abstract Trait Members
Traits support the use of abstract methods in order to impose requirements
upon the exhibiting class.
Express Requirements by Abstract Methods
getWorld();
}
abstract public function getWorld();
}
class MyHelloWorld {
private $world;
use Hello;
public function getWorld() {
return $this->world;
}
public function setWorld($val) {
$this->world = $val;
}
}
?>
]]>
Static Trait Members
Traits can define both static members and static methods.
Static Variables
inc(); // echo 1
$p = new C2(); $p->inc(); // echo 1
?>
]]>
Static Methods
]]>
Properties
Traits can also define properties.
Defining Properties
x;
?>
]]>
If a trait defines a property then a class can not define a property with
the same name unless it is compatible (same visibility and initial value),
otherwise a fatal error is issued.
Conflict Resolution
]]>