The new features introduced in 5.4 the traits which are mainly for horizontal code re-use and it’s a really a good revolution in PHP 5.4.Its actual purpose is to want to use multiple inheritance some occasions, but not any more PHP inheritance, so he invented such a thing.
Traits are proposed to reduce the gap of single inheritance with reusing of code or methods.The main reason for developing traits like functionality is to reduce the method complexity and avoid the problems related with multiple inheritance and Mixins.
Inheritance may take different forms like
- Single Inheritance (Only one Superclass)
- Hierarchical Inheritance (One Superclass ,Many subclasses)
- Multilevel Inheritance (Subclass derive from derived class)
- Multiple Inheritance (Several Superclass)
Single inheritance means each class can only extend one another and multilevel inheritance like we have one class say Flower and one more class Fruits so Fruits can extend Flower and new class Animal can extends Fruits class so we can say it a multilevel Inheritance.
Example of multilevel inheritance:
1 2 3 4 5 6 7 | Class Flower(){} Class Fruits extends Flower(){} Class Animal extends Fruits(){} |
From above example, we can say multilevel inheritance is for to reuse a code vertically and Traits is for to reuse a code horizontally.
As we know PHP does not support multiple inheritance because of this it’s create a code duplication and some improper hierarchies so Traits resolve all the problems generated by multiple inheritance and its reduce the complexity of code usually because of multiple inheritance.Traits allow us to build class hierarchies with extending functionality.
What is Traits and Syntax?
Traits can be understood as a set of different classes can call to the set of methods, but not the kind Traits! It can not be instantiated.
Let’s look at an example of the syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 | trait myTrait { } function traitMethod2 () {} } // Then call the traits, the syntax is:class myClass {use myTrait;} // this way you can use myTraits, call Traits method, and For example: $obj = new myClass(); $obj->traitMethod1(); $obj->traitMethod2(); |
NOTE: Traits is somewhat like Interface but with Horizontal reuse.
when we extend one class with another class, a subclass can have common functionality, methods like superclass. so inheritance provides us an access to all non-private code of a superclass with extend so we can say it a vertical reuse of code.
NOTES: If Superclass variable declares as PRIVATE, It can’t be accessible in subclass member.
Example:
Hope you get an idea about traits from it’s Syntax.Then, let’s explore use of traits with example
Let’s see a simple example of how to use traits:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | trait A { public function Hi() { echo 'Hi '; } } trait B { public function Users() { echo 'Bhumi '; } } class C { use A, B; public function Welcome() { echo 'Welcome'; } } $obj = new C(); $obj->Hi(); $obj->Users(); $obj->Welcome(); |
OUTPUT
Hi Bhumi Welcome
Here in above example I have created an object of class C and class C uses a traits A and B so we can call methods of both class with the use of object of class C. traits called by the use keyword in the class definition
Trait support abstract methods, support static methods, you can not directly define a static variable, but the method of static variables can be referenced trait.
Comments (2)