Loading...
The Power of Traits in PHP: Simplifying Common Property Injection

The Power of Traits in PHP: Simplifying Common Property Injection

Definition of traits in PHP

Traits in PHP are a way to reuse code in object-oriented programming. They let developers create reusable code that can be shared between classes without inheritance. Traits make adding specific behaviour to different types easy, allowing code to be reused across multiple class structures.

What is a Trait?

The trait is a valuable programming construct that allows for code reuse and provides a way to extend functionality in object-oriented programming languages.

But the real problem comes when

  • all or some styles have different parent classes (inheritance)!
  • Code fits poorly in the inheritance hierarchy.

 

Here trait comes to rescue our problem, as we all know that PHP does not support multiple inheritances,

So from PHP 5.4, we can use traits to reuse the sets of methods in various independent classes.

Here in this article, we will discuss using traits in PHP to overcome the problem of multiple inheritances.

The trait is a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

Use of trait

Here we will see a simple example of creating traits and using a standard set of methods in multiple classes using Trai.

php

<?php
   trait BundleClass{

       public function string_replace($search,$replace,$subject) {
           return str_replace($search, $replace, $subject);
       }

       public function in_array($value,$array) {
           return in_array($value, $array);
       }

   }

   // Here we have created a Simple trait BundleClass where we have set of common methods named  1)string_replace  2)in_array

   class ClassOne extends ParentClassOne{
       // Implement Trait
       use BundleClass;

       public function somefunction(){

           $string="lazy dog";

           // Use of Trait Class Function using $this
           $newString=$this->string_replace('dog','cat',$string);

           return $newString;

       }

   }

   class  ClassTwo extends ParentClassTwo{
       // Implement Trait Class 
       use BundleClass;

       public function somefunction(){

           $array=['red','blue','green'];

           // Use of Trait Class Function using $this
           $find=$this->in_array('red',$array);

           return $find;
       }
   }

$ClassOne=new ClassOne();
$ClassOne->somefunction();
//output: Lazy Cat

$ClassTwo=new ClassTwo();
$ClassTwo->somefunction();
//output: true

// In the above example you can see that both classes (ClassOne and ClassTwo) both having different parent classes inherited.

// To use our common set of methods we will simply add a trait using the “use BundleClass” keyword in our class.

//use of traits

Well, it was done by effectively implementing a robust PHP feature! Keep up the great coding efforts.

 

Contact us:+91 8128405131

Email send us at hello@elightwalk.com

Jayram Prajapati
Full Stack Developer

Jayram Prajapati brings expertise and innovation to every project he takes on. His collaborative communication style, coupled with a receptiveness to new ideas, consistently leads to successful project outcomes.

Most Visited Blog

Blog
How to Integrate a Custom Reducer into Your Website?

Effortlessly integrate a custom reducer into your website with our quick guide. Enhance your Magento PWA shop by introducing custom reducers for improved state management and user experience.

Read More
Blog
The Power of Traits in PHP: Simplifying Common Property Injection

Discover the power of PHP Traits! Our guide unveils the potential of characteristics by simplifying property injection and providing a simplified solution to typical code difficulties.

Read More
Blog
Magento 2 : Max Qty Allowed on Items Customization while update cart page

Set the maximum number of limitations for products on the update cart page to personalize Magento 2 experience. Learn how to customize the permitted on products to give customers personalized purchasing experience.

Read More