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:

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:

trait myTrait {

function traitMethod1 () {
}

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:

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

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

An Introduction to Kubernetes

Posted on 3 years ago

Bhumi

How To Repair MySQL Table

Posted on 7 years ago

Bhumi

Sliding Thumbnail and Caption with jQuery

Posted on 7 years ago

Bhumi

how you can add Voice Search in WordPress

Posted on 6 years ago

Bhumi

Hello world

Updated 5 days ago

Nick Carter

In depth Understanding of Laravel Core

Updated 2 months ago

Bhumi

ECMAScript 6 New Features – 2

Updated 1 year ago

Bhumi

The Reader’s Poll – June 2015

Posted on 4 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 4 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 5 years ago

Bhumi