PHP Traits
If you want to re-use some code in a single inheritance, So to implement this concept of code re-using in single inheritance is uses traits.
To use sets of class methods In single inheritance, It reduces some limitations of single inheritance.
It uses freely set of methods in independent classes living in different class hierarchies.
Why to use Traits
The combination of traits & classes complexity and avoids typical problems associated with single and multiple inheritance of an large scale application.
How to implement traits
A trait declaration is same as a class implementation, class uses class keyword to create a class but in traits. It uses trait keyword to implement traits.
But, It consist only group functionality or set of methods which will be declare in associated class.
<?php
trait ezcReflectionReturnInfo {
function getReturnType() { /*1*/ }
function getReturnDescription() { /*2*/ }
}
class ezcReflectionMethod extends ReflectionMethod {
use ezcReflectionReturnInfo;
/* ... */
}
class ezcReflectionFunction extends ReflectionFunction {
use ezcReflectionReturnInfo;
/* ... */
}
?>
Trait Precedence
If you are trying to declare a method which consist in parent class of trait, This sub-class will be override parent's method with own method. It follows serialize method to override methods.
There are current class of trait will be override previous class or parent class’s methods.
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.
<?php
class Base {
public function sayHello() {
echo 'Hello ';
}
}
trait SayWorld {
public function sayHello() {
parent::sayHello();
echo 'World!';
}
}
class MyHelloWorld extends Base {
use SayWorld;
}
$o = new MyHelloWorld();
$o->sayHello();
?>
Output:
Another example of traits precedence
<?php
trait HelloWorld {
public function sayHello() {
echo 'Hello World!';
}
}
class TheWorldIsNotEnough {
use HelloWorld;
public function sayHello() {
echo 'Hello Universe!';
}
}
$o = new TheWorldIsNotEnough();
$o->sayHello();
?>
Output:
How to implement & use multiple traits
<?php
trait Hello {
public function sayHello() {
echo 'Hello ';
}
}
trait World {
public function sayWorld() {
echo 'World';
}
}
class MyHelloWorld {
use Hello, World;
public function sayExclamationMark() {
echo '!';
}
}
$o = new MyHelloWorld();
$o->sayHello();
$o->sayWorld();
$o->sayExclamationMark();
?>
Output:
Conflicts multiple traits to each others at a time
If you have created two or more than traits and These consist some same name methods, Then this scenario of traits implements cause a FATAL ERROR.
Note: If you are using these traits in a same class then It will produce FATAL ERROR, otherwise It will run successfully.
How to resolve conflicts in traits
To resolve these conflicts, You need to use insteadof operator to choose exactly one of the conflicting methods.
Understand this situation through an example
<?php
trait A {
public function smallTalk() {
echo 'a';
}
public function bigTalk() {
echo 'A';
}
}
trait B {
public function smallTalk() {
echo 'b';
}
public function bigTalk() {
echo 'B';
}
}
class Talker {
use A, B {
B::smallTalk insteadof A;
A::bigTalk insteadof B;
}
}
class Aliased_Talker {
use A, B {
B::smallTalk insteadof A;
A::bigTalk insteadof B;
B::bigTalk as talk;
}
}
?>
Note:
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).
- Traits with visibility
- How to use the traits into another traits
- Traits Aabstract Methods
- Traits Static Methods
- Traits Properties