What is trait?
Trait 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 trait is going to remove the limitation of not having multiple inheritance.
Why Use trait?
The main concept behind the trait is the reusability of the code. trait 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 trait is to gain the benefits of multiple inheritance and alternatively of the code reusability.
How to Use trait?
Trait can be initialized with the keyword trait.
Let’s see how to start with the trait:
{
function first_method() { /* Code Here */ }
function second_method() { /* Code Here */ }
}
Trait can be used within class using use keyword.
{
// 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 trait
We can use multiple trait in a class. So here is what we can consider as a multiple inheritance.
So any class which uses the multiple trait will have all methods available in both trait.
Let’s see how it works:
{
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
Trait Contains the Trait
Here one more interesting thing is the we can use the trait inside another trait. We can say this as a extending the 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: trait can not be initialized with the keyword new same as class.
Abstract Methods in Trait
We can declare abstract method in trait. So after declaring the abstract method in trait, as class must have to implement that method which uses that trait. This is the same concept as the abstract class and abstract method.
{
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 Trait
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 trait 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:
{
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:
{
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
- trait integrate into the precedence order of method overriding.
- trait can not be initialized with the keyword new same as class.
- trait can be composed from trait.
- We can use multiple trait in one class.
- trait supports all modifiers like final, static and abstract.
- We can use insteadof and as operators to resolve the conflicts in trait.
Coclusion
The main the major benefit of using the trait is code reusability and multiple inheritamce like functionality.
It would be great if you share your thought on trait about how you are plannig to use this upcoming feature and what are the other benefits of using the trait.
Note: PHP 5.4 is not stable yet, So you can visit this link to run the PHP code using 5.4 version.
Pingback: PHP Magic Constants Explained | Expert PHP Developer
Pingback: New Features in PHP 5.4 | Expert PHP Developer
Pingback: PHP review???:trait | Who's William?
Pingback: Resoconto primo incontro | #pugMI
Pingback: PHP 5.4 ? TRAIT ???????? | ??
Pingback: Trait in PHP 5.4 ← Bookmarks