PHP uses trait to encounter a problem

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.