PHP traits

What is PHP Traits?

The concept of Traits is introduced in PHP 5.4, which is basically a group of functions which can be used in any other classes.
To overcome the problem of Multiple Inheritance, PHP introduced traits which is very much welcomed by PHP community.
A Trait is similar to a class, but only intended to group functionality. A trait starts with trait keywork instead of class. It encapsulates few methods which will be used in another method.

How can we create a Trait in PHP?

Syntax to create a trait in PHP:
trait MyFirstTrait{
public function traitMethod1(){
// code to do something
}
}

How can we use Trait Method inside a Class?

Traits Methods can be used in Class by using use keyword.
Syntax to use traits in class:
class SampleClass{
use MyFirstTrait; // use already created traits as above
public function classMethod(){
$this->traitMethod1();
}
}

Can we use more than one trait in a single Class?

Yes we can use multiple traits inside a single class
Sample code to use Multiple Traits :
trait MyFirstTrait{
public function traitMethod1(){ 
echo "traitMethod1";
} 
}
trait MySecondTrait{
public function traitMethod2(){ 
echo "traitMethod2";
} 
}
class SampleClass{ 
use MyFirstTrait, MySecondTrait; 
public function classMethod1(){ 
$this->traitMethod1(); 
}
public function classMethod2(){ 
$this->traitMethod2(); 
} 
}

What if two traits contain methods of same name?

If two traits contain methods of same name then PHP will throw fatal error.
We can solve this condition by resolving the conflict using insteadof keyword.
trait MyFirstTrait {
public function traitMethod() {
echo 'Hello World1!';
}
}
trait MySecondTrait { 
public function traitMethod() {
echo 'Hello World2!';
}
}
class SampleClass{
use MyFirstTrait, MySecondTrait{
MyFirstTrait::traitMethod insteadof MySecondTrait;
}
public function classMethod(){
echo $this->traitMethod();
}
}
$sampleObj = new SampleClass();
$sampleObj->classMethod();

Can a trait use another trait?

Yes, we can use a trait inside another trait.
trait MyFirstTrait{
public function traitMethod1(){
echo "traitMethod1";
}
}
trait MySecondTrait{
public function traitMethod2(){
echo "traitMethod2";
}
}
trait MyThirdTrait{
use MyFirstTrait, MySecondTrait;
}
class SampleClass{
use MyThirdTrait;
public function classMethod1(){
$this->traitMethod1();
}
public function classMethod2(){
$this->traitMethod2();
}
}
$sampleObj = new SampleClass();
$sampleObj->classMethod1();
$sampleObj->classMethod2();
Some more points to remember while using Traits
trait MyFirstTrait{ 
public function traitMethod1();
}
  • We can change the visibility of traits methods inside a class where we are using traits.
trait MyFirstTrait{ 
public function traitMethod1(){// do something};
}
class SampleClass{
use MyFirstTrait{ traitMethod1 as protected;}
}
  • We can also define a variable inside traits which can be used by a class.
trait MyFirstTrait{ 
public variable1 = "Hello World";
}
Traits are not to allowed to instantiate, It can be used with class only. But using too many traits with a class, it will increase the unnecessary responsibilities of a class which is not needed and not desirable for the coding standard.  I hope you enjoyed the articles. Please comment if you like the articles

Labels: ,

© copyright-2020 Rejaul