Detailed Trait

Because php is a single inheritance language, can not inherit properties and methods from two base classes, in order to solve this problem, the php after 5.4 Trait this feature.

php text: Trait is a mechanism for code reuse single inheritance language similar to PHP's prepared. Trait In order to reduce single inheritance language restrictions, allowing developers to freely separate class multiplexing method in a different hierarchy. Trait semantic definitions and combinations of Class A way to reduce complexity, avoid the traditional typical problems and multiple inheritance classes Mixin related.

See Example:

1.

trait Dog{
        public $name = 'Dog';
        public function drive(){
                echo 'This is trait dog drive';
        }
        public function eat(){
                echo 'This is trait dog eat';   
        }
}

class Animal{
        public function drive(){
                echo 'This is class animal drive';
        }
        public function eat(){
                echo 'This IS class Animal EAT ' ; 
        } 

} 

class Cat the extends Animal { 
        use Dog; 
        public function Drive () { 
                echo ' This IS class CAT Drive ' ; 
        } 

} 

$ CAT = new new Cat (); 
$ CAT -> Drive (); 
echo " <br> " ; 
$ CAT -> EAT (); 

// output
 // This class CAT iS Drive
 // This trait Dog EAT iS
 // Conclusion: override base class / method calls trait or property the same name;

2.

trait trait1{
        public function eat(){
                echo "This is trait1 eat";
        }
        public function drive(){
                echo "This is trait1 drive";
        }
}
trait trait2{
        public function eat(){
                echo "This is trait2 eat";
        }
        public function drive(){
                echo "This is trait2 drive";
        }
}
class cats{
        use trait1,trait2{
                trait1::eat insteadof trait2;
                trait1::drive insteadof trait2;
        }
}
class dogs{
        use trait1,trait2{
                trait1::eat insteadof trait2;
                trait1::drive insteadof trait2;
                trait2::eat as eaten;
                trait2::drive as driven;
        }
}
$cats = new cats();
$cats->eat();
echo "<br>";
$cats->drive();
echo "<br>";
echo "<br>";
echo "<br>";
$dogs = new dogs();
$dogs->eat();
echo "<br>";
$dogs->drive();
echo "<br>";
$dogs->eaten();
echo "<br>";
$dogs->driven();

//This IS trait1 EAT
//output
//IS trait1 Drive This 


// This IS trait1 EAT
 // This IS trait1 Drive
 // This IS trait2 EAT
 // This IS trait2 Drive
 // Conclusion: The trait can be used, the interval; when different trait called, has the same properties or methods, will conflict, may be used INSTEAD (be substituted), or as (its alias)

3.

Animal {trait
         public function EAT () { 
                echo " This Animal EAT IS " ; 
        } 
} 

class Dog { 
        use Animal { 
                EAT AS  protected ; 
        } 
} 
class Cat { 
        use {Animal 
                Animal :: EAT AS  Private eaten; 
        } 
} 
$ Dog = new new Dog ();
 // $ DOG> eat (); // error, as it has been changed to protect the eat 

$ CAT = new new Cat (); 
$ CAT -> EAT (); // normal operation, does not modify the original access control 
$ cat-> eaten (); // error, has been turned into a private access control 

// Conclusion: as you can modify the access method control

4.

trait Cat{
        public function eat(){
                echo "This is Cat eat";
        }
}
trait Dog{
        use Cat;
        public function drive(){
                echo "This is Dog drive";
        }
        abstract public function getName();

        public function test(){
                static $num=0;
                $num++;
                echo $num;
        }

        public static function say(){
                echo "This is Dog say";
        }
}
class animal{
        use Dog;
        public function getName(){
                echo "This is animal name";
        }
}

$animal = new animal();
$animal->getName();
echo "<br/>";
$animal->eat();
echo "<br/>";
$animal->drive();
echo "a " ; 
$ :: Animal say (); 
echo " a " ; 
$ Animal -> Test (); 
echo " a " ; 
$ Animal -> Test (); 
echo " <br /> " ; 
$ Animal -> Test (); 

// output
 // This IS Animal name
 // This Cat EAT IS
 // This Dog Drive IS
 // This Dog say IS
 // . 1
 // 2
 // . 3
 // Conclusion: Trait may be combined with each other, abstract methods, static properties, static methods and the like can also be used

source:

https://www.jianshu.com/p/fc053b2d7fd1

Guess you like

Origin www.cnblogs.com/two-bees/p/11353960.html