What are Traits in PHP

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.

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

ClassFlower(){}

ClassFruits extendsFlower(){}

ClassAnimal extendsFruits(){}

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.

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

traitA{

publicfunctionHi(){

echo'Hi ';

}

}

traitB{

publicfunctionUsers(){

echo'Bhumi ';

}

}

classC{

useA,B;

publicfunctionWelcome(){

echo'Welcome';

}

}

$obj=newC();

$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

Post navigation

About Author

Bhumi

Bhumi shah is currently working with leading web development company as a Software Analyst and also the founder of the Creativedev. She has immense interest in programming and web designing.She is passionate about technical blogging and almost versatile in terms of programming across various languages & frameworks such as PHP,MySQL WordPress, Twitter Bootstrap, AngularJS, HTML5, CSS3, jQuery and more.