PHP is a single-inheritance language, and a class can inherit only a single primitive class
Since PHP5.4.0, PHP has implemented a code reuse method called trait
Trait are specific properties or methods that should be available in some classes (class), while other classes of the half-class should avoid using these properties and methods.
Class member priority is: Current class >Trait> parent class
Trait Funca {2// two, 3 ///trait the use of keywords is the same as class definition 4 //is also a 5 public function A () {6 echo "he" that can define methods and properties) Llo " } 8 public function B () {9 echo ' word ' }11 public Function mm () {echo" MMP " c16>}14 Public Function C () {$this->a(), $this->b }19 class a {20////using the USE keyword in Class A enables Multiplexing F UNC methods and properties (in disguise inherit the properties and methods in Func) use }23 $a = new a (); $a->c ();
The above code, the final output of the result is Hello Hello word
After debugging the breakpoint, it was found that the A method in the above code was executed when the object was instantiated (is it a coincidence?). Change posture in a test once)
Trait Funca {2// two, 3 ///trait the use of keywords is the same as class definition 4 //is also a 5 public function A () {6 echo "he" that can define methods and properties) Llo " } 8 public function B () {9 echo ' word ' }11 public Function C () {$this->
a (); $this-
}16 class B {17//using the USE keyword in Class A, you can implement the methods and properties in the multiplexed func (in disguise inherit the properties and methods in Func). Use }20 $a = new B ();//$a->c ();
According to the truth should be nothing output, unfortunately, in the instantiation of the trait in the B method is still executed (then we change a posture to test it?) )
Trait Funca {// II, ///trait the use of keywords is the same as the class definition //is also a public function that can define methods and properties test1 () { echo "Hello" ; } Public Function Test2 () { echo "word"; } Public function C () {$this->test1(); $this->test2();}} class a {///using the USE keyword in Class A enables the reuse of Func The methods and properties of polygons (in disguise inherit the properties and methods in Func) use Funca;} $a = new A ();
According to our previous inference, this time should not output anything!! That's right, this time, as we expected, so why is that? I don't know, either. Wait for the great God to answer questions .....
PHP uses trait to encounter a problem