Let’s explore PHP traits, their usage, and how they differ from interfaces and abstract classes.
What are Traits?
A trait lets you reuse code. A Trait is similar to a class, but it is not possible to instantiate it. It enables horizontal composition of behavior. Traits are used to define methods that can be utilized in multiple classes.
Traits are declared with the trait
keyword:
trait GreetingTrait {
public function sayGoodbye() {
echo "Goodbye! ";
}
}
class MyClass {
use GreetingTrait;
}
$obj = new MyClass();
$obj->sayGoodbye(); // Output: Goodbye!
Traits are a mechanism for code reuse in single inheritance languages, which means that a child class can inherit from only one parent class. However, if a class needs to inherit multiple behaviors, this limitation can be overcome using traits.
Another possible use:Traits can also be used to split a large php file. Ideally in this case, several classes should be recreated, but while waiting for a major rework, the Traits can help with readability (It’s not pleasant to have to scroll like crazy…).
What can Traits contain?
PHP traits can contain many of the elements of a PHP class:
- Properties and methods with visibility operators (public, protected, private)
- Static properties and methods
- Class operators, such as parent::, self:: and $this
- Abstract methods
- Constants properties
- Traits!
When to use a trait?
Traits have similarities with interfaces and with abstract classes. Let’s see the differences.
Traits vs Interfaces
Interfaces allow multiple classes to share the same methods, but they are limited to abstract methods and do not support code reuse. On the contrary, traits offer fully implemented methods, properties, and class operators, allowing classes to reuse trait code without duplication.
Traits vs Abstract Classes
Both traits and abstract classes can include abstract or fully implemented methods.
Extending an abstract class is required to access its methods in another class.
While establishing an inheritance relationship with an abstract class is necessary to utilize its methods, it may not always be preferable.
PHP lacks support for multiple inheritance, which restricts the creation of different combinations through separate abstract classes.
However, traits offer a solution by enabling the simultaneous usage of multiple traits within the same class.
Conflict Resolution
PHP traits support aliasing. You can alias a trait’s method to change its name and even change the visibility!
trait GreetingTrait {
public function sayGoodbye() {
echo "Goodbye! ";
}
}
class MyClass {
use GreetingTrait {
sayGoodbye as farewell;
}
}
$obj = new MyClass();
$obj->farewell(); // Output: Goodbye!
If two Traits have a method with the same name, then there is a conflict and a fatal error is produced, if the conflict is not explicitly resolved.
You must explicitly solve the conflict the insteadof
operator, which allows to choose exactly one of the conflicting methods .
trait GreetingTrait {
public function sayGoodbye() {
echo "Goodbye! ";
}
}
trait SalutationTrait {
public function sayGoodbye() {
echo "See you later! ";
}
}
class MyClass {
use GreetingTrait, SalutationTrait {
SalutationTrait::sayGoodbye insteadof GreetingTrait;
GreetingTrait::sayGoodbye as farewell;
}
}
$obj = new MyClass();
$obj->sayGoodbye(); // Output: See you later!
$obj->farewell(); // Output: Goodbye!
Learn more PHP: Traits
Leave a Reply