articles

Home / DeveloperSection / Articles / Traits in PHP

Traits in PHP

Mark Devid 2084 22-Jun-2016

In this section we will learn about the following topics:

·         What is trait?

·         How to implement trait?

·         Difference between trait and interface

·         Difference between trait and abstract class

·         Benefits of trait

What are traits in PHP?

In real life traits are defined as the movement of characteristics from parent to child .In programming terms, trait is used for code re-usability. As we know PHP is a single inheritance programming language which has a limitation of code re-usability. The main aim of traits is to reduce the limitation of code re-usability in PHP.

A trait in PHP is the group of methods that you want to include within a class. These groups of methods can be re declared in various classes and if you have different classes which uses same methods then you can use these methods to group them and create PHP traits.  Like abstract class, trait cannot be instantiated. Following is an example which shows how it works:

Note: Trait in PHP is introduced in 5.4. before 5.4 version of PHP uses abstract class and interface. Later we will discuss the difference between interface versus trait and abstract class versus trait.

Code implementation:
 

<?php
trait Myclass //Myclass is PHP Trait
{
public function my_trait_function() //Fuction my_trait_function is the function which will be re used by term class {
echo "This is my First trait function."."</br>";
}
}
class term // term is the class where trait is used
{
use Myclass;
public function hello()
{
echo "This is class method.";
}
}
$obj = new term();
$obj->my_trait_function();
$obj->hello();
?>

 Note:As in the above example Myclass is the trait which has my_trait_function which is reused by the by the class term.

Code output:
This is my First trait function.
This is class method.

 

Benefits of Traits in PHP

PHP is a single inheritance programming which has a limitation of re use code. So, the major benefit of using trait is that it provides code reusability as well as code duplication.

How are traits different from abstract classes?

Abstract classes are those classes which cannot be instantiated and require subclasses to provide implementation for the abstract methods or in other words you cannot create object of abstract classes and these classes are used for inheritance purpose. Abstract classes can only inherited in your child class whereas a trait does not rely on inheritance. Following is an example of abstract class:

Code implementation:

<?php
abstract class My_parent
{
abstract public function text();
}class Child extends My_parent
{
public function text()
{
echo "Hello World!! I am child class";
}
}
$obj = new Child();
$obj->text();
?>

 Note: Myparent is an abstract class with abstract function text() which is implemented by Child class. Please note if you have an abstract method in abstract class and you have extend your abstract class then it is mandatory to declare your abstract method and if you don’t declare your abstract method then in that case PHP will throw error.

Code Output:
Hello World!! I am child class

How is trait different from interface?

Interface is a contract says “this object can do this things.” whereas trait is giving “an ability to do things.” following is an example of interface and trait:

Code implementation:  
<?php
interface myname
{
public function fname();
}
trait Myclass //Myclass is PHP Trait
{
public function my_trait_function() //Fuction my_trait_function is the function which will be re used by term class {
echo "This is my First trait function."."</br>";
}
}
class term implements myname // term is the class where trait is used
{
use Myclass;
public function hello()
{
echo "This is class method."."</br>";
}
public function fname()
{
echo "This is an example of interface"; // fname is the function of myname interface
}
}
$obj = new term();
$obj->my_trait_function();
$obj->hello();
$obj->fname();
?>

Note:  myname is an interface and fname is the function. Myclass is the trait and my_trait_function is the method of trait.

Code Output:
This is my First trait function.
This is class method.
This is an example of interface

php php 
Updated 19-Dec-2017

Leave Comment

Comments

Liked By