Traits in PHP – Multiple and Nested Traits

Traits is a good new addition to PHP language. In our series about the new features of PHP 5.4, we reviewed the concept of Traits in PHP. The introductory article talks about what the traits are and what is the general syntax of Traits in PHP. The second article attempts to explain why we need traits.

Now, we are familiar with creating a Trait and using it. However, there is much more that you can do with Traits:

  1. Abstract Methods in Traits.
  2. Nested Traits.
  3. Multiple Traits in a PHP Class.

Define Abstract Methods in Traits

The concept of Abstract methods in traits is similar to the concept of Abstract class or Abstract method. An abstract method can be declared in the trait by using the abstract keyword. In this case, the class using the trait has to implement the definition of the abstract method.

// Trait Address

trait Address

{

// The class which uses this trait will have to implement this method

abstract public function getAddress();

public function showAddress() {

echo 'The address is: '.$this->getAddress();

}

}

// Class Business

class Business

{

private $address;

use Address;

// Class implements the Abstract method of Trait

public function getAddress() {

return $this->address;

}

public function setAddress($value) {

$this->address  = $value;

}

}

$obj = new Business();

$obj->setAddress('India');

$obj->showAddress();

//Output: The address is: India
Update: Fixed one typo and removed one un-necessary argument from the showAddress() declaration. Thanks to nikolas.lotz. Also, added the code that uses the defined Class (Business) and Trait (Address).

Nested Traits in PHP

As already read in the introductory article, a trait cannot be instantiated and used like a Class. However, it can be used within a class or another trait. The way a trait is used in a class, in the same way, the trait can be used in another trait. Thus, a trait A can be nested into another trait B and the methods and members of A can be accessed in the trait B and in the class using trait B.

trait trait1 {

public function trait1_method1() {

}

}

trait trait2 {

use trait1;

public function trait2_method1() {

}

}

class class1 {

use trait2;

}

$obj = new class1();

$obj->trait2_method1();  // Method of trait2

$obj->trait1_method1(); // Method of trait1

Multiple Traits in PHP

To use multiple traits, you don’t always need to nest them. You can easily use multiple traits by comma-separating their names in the class.

trait trait1 {

public function trait1_method1() {

}

}

trait trait2 {

public function trait2_method1() {

}

}

class class1 {

use trait1, trait2;

}

$obj = new class1();

$obj->trait2_method1();  // Method of trait2

$obj->trait1_method1(); // Method of trait1

Traits aim to increase code reusability by adding the multiple inheritance like functionality in PHP. You can use them in any way that suits your requirements.

The PHP 5.4 is stable and latest version can be downloaded from the official PHP website.