A
trait
is a group of methods that can be inserted into classes. They were added in PHP 5.4 to enable greater code reuse without the added complexity that comes frosm allowing multiple inheritance. Traits are defined with the
trait keyword, followed by a name and a code block. The naming convention is commonly the same as for classes, with each word initially capitalized. The code block may only contain static and instance methods.
trait PrintFunctionality
{
public function myPrint() { echo 'Hello'; }
}
Classes that need the functionality that a trait provides can include it with the
use keyword
, followed by the trait’s name. The trait’s methods then behave as if they were directly defined in that class.
class MyClass
{
// Insert trait methods
use PrintFunctionality;
}
$o = new MyClass();
$o->myPrint(); // "Hello"
A class may use multiple traits by placing them in a comma-separated list. Similarly, a trait may be composed of one or more other traits.
Inheritance and Traits
Trait methods override
inherited methods. Likewise, methods defined in the class override methods inserted by a trait.
class MyParent
{
public function myPrint() { echo 'Base'; }
}
class MyChild extends MyParent
{
// Overrides inherited method
use PrintFunctionality;
// Overrides trait inserted method
public function myPrint() { echo 'Child'; }
}
$o = new MyChild();
$o->myPrint(); // "Child"
Trait Guidelines
Single inheritance sometimes forces the developer to make a choice between code reuse and a conceptually clean class hierarchy. To achieve greater code reuse, methods can be moved near the root of the class hierarchy, but then classes start to have methods that they do not need, which reduces the understandability and maintainability of the code. On the other hand, enforcing conceptual cleanliness in the class hierarchy often leads to code duplication, which may cause inconsistencies. Traits provide a way to avoid this shortcoming with single inheritance by enabling code reuse that is independent of the class hierarchy.