Back to Top

What are Traits in PHP

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.

Inheritance may take different forms like

  1. Single Inheritance (Only one Superclass)
  2. Hierarchical Inheritance (One Superclass ,Many subclasses)
  3. Multilevel Inheritance (Subclass derive from derived class)
  4. 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:

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:

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:

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

NOTES: Traits support all modifiers final, static and abstract.

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 (1)

  1. AliseCBoor says:

    Everything is very open having a clear explanation from the challenges.

    It was actually truly informative. Your internet site is beneficial.
    Thanks for sharing!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Blank Screen of WordPress Admin Panel

Posted on 6 years ago

Bhumi

How to add Tags in WordPress Pages?

Posted on 5 years ago

Bhumi

How to use Session in Joomla

Posted on 6 years ago

Bhumi

Tips to Choose Your Next WordPress Theme

Posted on 5 years ago

Bhumi