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)
- Interface can’t contain any properties. Only constants are allowed
- Method can’t contain body
- Only public methods are allowed. Private and public is not allowed
- 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
- Its like general class extends that have properties, methods in base class and can easily used like extend functionality.
- We can use multiple traits as use A, B, C
- We can override traits function from calling class .
- 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
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.
Chandra Shekhar
Latest posts by Chandra Shekhar (see all)
- Best practices for micro service design - January 23, 2022
- Spring Boot - January 23, 2022
- Java - January 23, 2022
Recent Comments