Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Traits in Php 5.4

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 175
    Comment on it

    Hello Readers! Here is a small blog on traits. What is trait? What are its uses? Why it is implemented?.. In this blog all these questions will be covered.

     

    A trait, in simple language, is a group of methods that we would include within another class. In PHP, we have limitations of single inheritance and traits are the mechanism for code reuse. Traits were added as new feature of the language in PHP 5.4. With the use of traits we can overcome the problems of multiple inheritance and we can also reduce the duplication of the code.

     

    An example of trait could be:

         trait Share{
                 public function sharing()
    		{
    			echo  Share trait!;
    		}
    	}

     

    we can then include above trait within other classes like :

    
    	class show
    		{
    			use Share;
    		}
    	class result
    		{
    			use Share;
    		}
    

     

    Next, in case if you create new objects of these classes you will find that they both have the sharing() method available

    	$obj1 = new show;
    	echo $obj ->sharing();  // Share trait!
    
    	$obj2 = new result;
    	echo $obj2->sharing();  // Share trait!

     

    A trait is just a way to “copy and paste” code during run time.

     

    <?php
    
    class Parent
    
    {
    
    	public function ParentFunction()
    	
    	{
    
    		echo Parent Function called!;
    	
    	}
    }
    
    trait abc
    
    {
    	
    	public function TraitFunction()
    	
    	{
    
    		echo Trait Function called!;
    
    	}
    
    }
    
    class Child extends Parent
    
    	{
     		use abc;
    	}
    
    $obj = new Child();
    
    $obj -> ParentFunction();
    
    $obj -> TraitFunction();
    
    ?>
    

     

    Output will be:

    Parent Function called!

    Trait Function called!

     

    From this example it is cleared that trait is much similar to classes but it is not possible not instantiate a Trait on its own. Next important thing to discuss is the priority like we have some function in class and we override the same function in our trait then what will be the output of the program? Let us check it out through this example.

     

    <?php
    
    	class Parent 
    	
    	{
    
    		public function ParentFunction()
    		{
    			echo Parent Function called!;
    		}
    	}
    
    	trait abc
    	{
    
    		public function ParentFunction()
    		{
    
    			echo Trait Function called!;
    		}
    	}
    
    	class Child extends Parent
    	{
     		use abc;
    	}
    
    $obj = new Child();
    
    $obj -> ParentFunction();
    
    ?>
    

     

    Output will be:

    Trait Function called!

     

    Hope till now the concept of trait is cleared. That's all for this blog will continue this topic further in next blog.

    Happy Coding.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: