Recent Post

Concept Of Traits

 

Traits are used to declare methods that can be used in multiple classes.  A trait is like class. Traits reduces the complexity, and avoids problems associated with multiple inheritance and Mixing. Note that PHP does not allow multiple inheritance. So Traits is used to fulfill this gap by allowing us to reuse same functionality in multiple classes.

Traits Containing:

·         Methods

·         Abstract methods

·         The methods can have any access modifier

 

o   Public,

o   Private,

o   Protected

Code is Here

<?php
class Dish{
    public function vegetables(){
        echo "I am from vegetable";
    }

    public function bread(){
        echo "I am from bread";
    }
}

trait sweet{
    public function sweet(){
        echo "I am from sweet Dish on trait";
    }
}

class Dish2 extends Dish{
    use sweet;
}
$objDish2 = new Dish2();
$objDish2->sweet();

?>
Output Here
I am from sweet Dish on trait