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.

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...