Traits in PHP 5.4 - Avinash Zala - Web Development Expert

Traits in PHP 5.4

What is traits?

Traits is the new concept introduced in PHP 5.4. Which likes to remove the limitation of the multiple inheritance in single inheritance language PHP.

Before PHP 5.4, PHP support single inheritance and multiple interface but traits is going to remove the limitation of not having multiple inheritance.

Why Use traits?

The main concept behind the traits is the reusability of the code. traits seems to be very useful in terms of code reusability for the in language which supports only single inheritance like PHP.

So main reason to use the traits is to gain the benefits of multiple inheritance and alternatively of the code reusability.

How to Use traits?

Traits can be initialized with the keyword trait.

Let’s see how to start with the trait:

1
2
3
4
5
  trait first_trait
  {
    function first_method() { /* Code Here */ }
    function second_method() { /* Code Here */ }
  }

Traits can be used within class using use keyword.

1
2
3
4
5
6
7
8
9
10
11
12
  class first_class
  {
    // Using the Trait Here
    use first_trait;
   
  }
 
  $obj = new first_class();
 
  // Executing the method from trait
  $obj->first_method(); // valid
  $obj->second_method(); // valid

Make note that we can not create an object of the class using new keyword just like class.

Using Multiple traits

We can use multiple traits in a class. So here is what we can consider as a multiple inheritance.

So any class which uses the multiple traits will have all methods available in both traits.

Let’s see how it works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  trait first_trait
  {
    function first_method() { echo "method1"; }
  }
 
  trait second_trait
  {
    function second_method() { echo "method2"; }
  }

  class first_class
  {
    // now using more than one trait
    use first_trait, second_trait;
  }
 
  $obj= new first_class();
 
  // Valid
  $obj->first_method(); // Print : method1
 
  // Valid
  $obj->second_method(); // Print : method2

Traits Contains the Trait

Here one more interesting thing is the we can use the traits inside another traits. We can say this as an extending the traits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  trait first_trait
  {
    function first_method() { echo "method1"; }
  }
 
  trait second_trait
  {
    use first_trait;
    function second_method() { echo "method2"; }
  }

  class first_class
  {
    // now using
    use second_trait;
  }
 
  $obj= new first_class();
 
  // Valid
  $obj->first_method(); // Print : method1
 
  // Valid
  $obj->second_method(); // Print : method2

Note: traits can not be initialized with the keyword new same as class.

Abstract Methods in Traits

We can declare abstract method in traits. So after declaring the abstract method in traits, as class must have to implement that method which uses that traits. This is the same concept as the abstract class and abstract method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  trait first_trait
  {
    function first_method() { echo "method1"; }
    // any class which use this trait must
    // have to implement below method.
    abstract public function second_method();
  }
 
  class first_method
  {
    use first_trait;
   
    function second_method()
    {
      /* Code Here */
    }
  }

Note: trait can be composed from trait.

Conflicts in Traits

There can be a case, if any class using the more than one trait and two trait have same methods defined. In that case if you use two traits then it will give you Fatal Error.

But actually we can instruct the compiler for which method to use. This can be done using insteadof operator.

Let’s have an example for this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  trait first_trait
  {
    function first_function()
    {
      echo "From First Trait";
    }
  }

  trait second_trait
  {
    function first_function()
    {
      echo "From Second Trait";
    }
  }

  class first_class
  {
    use first_trait, second_trait
    {
      // This class will now call the method
      // first function from first_trait only
      first_trait::first_function insteadof second_trait;
    }
  }  
 
  $obj = new first_class();
  // Output: From First Trait
  $obj->first_function();

So now we have removed the confliction if the same function names. But what if you still want to use the methods of the both traits?

So in that case you can have as operator to the rescue. This operatoe is used for aliasing. Let’s see how it works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  trait first_trait
  {
    function first_function()
    {
      echo "From First Trait";
    }
  }

  trait second_trait
  {
    function first_function()
    {
      echo "From Second Trait";
    }
  }

  class first_class
  {
    use first_trait, second_trait
    {
      // This class will now call the method
      // first function from first_trait only
      first_trait::first_function insteadof second_trait;
     
     
      // first_function of second_traits can be
      // accessed with second_function
      second_trait::first_function as second_function;
     
    }
  }  
 
  $obj = new first_class();
  // Output: From First Trait
  $obj->first_function();
 
  // Output: From Second Trait
  $obj->second_function();

Points to Keep in Mind About trait

  1. traits integrate into the precedence order of method overriding.
  2. traits can not be initialized with the keyword new same as class.
  3. traits can be composed from traits.
  4. We can use multiple traits in one class.
  5. traits supports all modifiers like final, static and abstract.
  6. We can use insteadof and as operators to resolve the conflicts in traits.

Coclusion

The main the major benefit of using the traits is code reusability and multiple inheritamce like functionality.

It would be great if you share your thought on traits about how you are plannig to use this upcoming feature and what are the other benefits of using the traits.

Note: PHP 5.4 is not stable yet, So you can visit this link to run the PHP code using 5.4 version.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by: