Why Traits in Magento 2

 

In PHP, Multiple inheritence was supported with the help of Interface. But interface implementation is not flexible like class inheritance.

So to make this happen traits came into the picuture. We will understand this concept with below :-

Why Inheritance introduced in OOPS

With the help of inheritance we make our code re-usable, it means same property and methods doesn’t needs to be re-written in every class. We only need to extend that base class and every thing would be accessible (of course not private one) in child class.But the only limitation is that we can’t apply more than one extend in a class. To overcome this problem interface has come into picture so in the nutshell interfaces are the OOPs concept that is used to support multiple inheritance. See it in example of interface.


/**
Multiple inheritence was supported in the form of Interface.
Below is the example of an interface
*/
Interface Employee
{
const name="shekhar";
//Protected $age;// Not allowed in interface
//Private $salary; // Not allowed in interface

public function  getName();
public function getAge();

public function getSalary();

}

Interface Employee2
{
public function getMaritalStatus();
}

class Engineer implements Employee
{
public function GetName()
{
echo "GetName() of Engineer class worked";
}
public function getAge(){

}
public function getSalary()
{

}
public function getMaritalStatus()
{

}
}
$objEngineer = new Engineer();
$objEngineer ->GetName();

 

 

But there are certain Limitations of Interface (difference between inheritance and interface)

  1. Interface can’t contain any properties. Only constants are allowed
  2. Method can’t contain body
  3. Only public methods are allowed. Private and public is not allowed
  4. All the methods of Interface must be defined into their child class

 

 

What is Traits

To overcome the problem of Interface Traits is (a new feature) introduced in PHP 5.4. Traits support multiple inheritance like Interface but it has improved all the limitations.

 

Advantage of using Trait

  1. Its like general class extends that have properties, methods in base class and can easily used like extend functionality.
  2. We can use multiple traits as use A, B, C
  3. We can override traits function from calling class .
  4. It gives the way to create cleaner, simpler, and more efficient code for implementing complex problem.

See it in Example of trait


trait Employee
{
public $name;
Protected $age;
Private $salary;

public function GetName()
{
echo "GetName() from Trait is calling";
}
public function getAge(){
echo "getAge() from Trait is calling";
}
public function getSalary()
{
echo "getAge() from Trait is calling";
}

}

trait Employee2
{
public function getMaritalStatus()
{
echo "Calling from getMaritalStatus trait";
}
}

class Engineer
{
use Employee, Employee2;

public function getAge(){

}
public function getSalary()
{

}

}
$objEngineer = new Engineer();
$objEngineer ->GetName();
$objEngineer ->getMaritalStatus();

What are the hate points for Trait


There are nothing in the world that can’t contain hate points, traits are not exception of it.
Let see some hate stories of traits.
*. The trait class code might not visually present on the class where its being utilized.

*. One error in trait might throw error in another class where its not been utilized. So, it makes it confusion.

Don’t worry its not only the problem with trait only. Its the problem with OOPs implementation theory. So no worrys we will talk about their good things only.

 

Where does traits are being used in Magento

There are not only multiple vendor extensions that are using traits but also even there are certain magento framework classes that itself uses traits. like Interceptor class, under Magento test framework, classMapGenerator.. etc.

Related posts:

The following two tabs change content below.

Chandra Shekhar

Consultant at Infogain
Chandra Shekhar is a LAMP Developer,Thinker,Blogger and currently working as Magento(Ecommerce) specialist.. I am always open to learn new things in LAMP as well as other newer technology. In the journey of my last 9 years of web and product development, I have decided to share my knowledge and experience to Open Source Community in 2011. In this regard I have started CuteHits.com. Since then I am actively sharing my knowledge on various LAMP technology through CuteHits.com’s technical ... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)