PHP and MySQL (54 Blogs) Become a Certified Professional

All You Need to Know About traits in PHP

Published on Sep 16,2019 32 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Before we understand traits in PHP. We have learned about inheritnce that means a class can extend other class. Let’s say, class B extends class A and class C is also extending class A which means function defined in class A can be accessed by class B and class C but suppose if class B and class C has overridden function X and let’s say class D is extending both classes.

traits-in-php

Although multiple inheritances do not support it by PHP but suppose it would be a case then, Which function does class D will run? Is it from class B or Is it from class C? So this is actually multiple inheritance problem, that’s why PHP does not support it but there are many cases where you want to extend more than one classes for code reusability.

traits-in-php-2

 

When we talk about single inheritance problem, class D extends class C, but what if we want to have some functions of class B in class D. For this we can use traits that are introduced in PHP 5.4. It is simple and easy to create. It is just like a class but only for a group of methods and like abstract classes, you cannot instantiate discretes.

       

       

      Traits in PHP

      Generally, They can define both static members and static methods which helps developers to reuse methods freely in several independent classes in different class hierarchies. Traits avoid problems associated with multiple inheritances, Mixins and also reduces complexity.

      Syntax:

      <?php
      // syntax for trait in PHP
      Trait sample
      {
      function ReturnType()
      {
      }
      function ReturnDescription()
      {
      }
      }
      ?>
      

      Traits can be included within other classes in this way.

      class Post
      {
      use Sharable;
      }
      class Comment
      {
      use Sharable;
      }
      

      As PHP does not allow multiple inheritances, Trait is used to overcome this by allowing us to reuse the same functionality in multiple classes. The below example demonstrates the working of traits in PHP.

      
      <?php
      trait Sample
      {
      public function func()
      {
      echo "trait";
      }
      }
      
      class myClass
      {
      use sample;
      public function func()
      {
      echo "class";
      }
      }
      
      $obj = new myClass();
      $obj->func();
      ?>
      
      

      Output:

      Output-traits

       

      Advantage of Traits

      Traits reduce code duplication whilst preventing complicated class inheritance that might not make sense within the context of your application.

      advantages-traits

      This helps to allow define simple Traits that are clear and concise and then mix in that functionality where appropriate.

       

      Traits vs Interfaces in PHP

      Generally, the main difference between the Interfaces and Traits in PHP is that the Traits basically define the actual implementation of each method within each class, so the same interface is implemented by many classes but having different behavior, while traits are just chunks of code injected in a class in PHP.

      <?php
      interface SampleInterface
      {
      // function...
      }
      ?>
      

      With this, we come to an end of these traits in python article. I hope you have learned about Traits, Advantage of Traits and difference between Traits and Interfaces.

      If you found this PHP blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

      Got a question for us? Please mention it in the comments section of ”Traits in PHP” and I will get back to you.

      Comments
      0 Comments

      Browse Categories

      webinar REGISTER FOR FREE WEBINAR
      REGISTER NOW
      webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

      Subscribe to our Newsletter, and get personalized recommendations.