PHP7 's Trait detailed

Source: Internet
Author: User

Transferred from: HTTPS://WWW.JIANSHU.COM/P/FC053B2D7FD1

PHP has been a single-inheritance language from the past to the present, unable to inherit properties and methods from two base classes at the same time, in order to solve this problem, PHP has trait this feature (I heard that this trait and go language a bit similar, specifically did not learn the go language)

Usage: By using the USE keyword in a class, declare the trait name to be combined, use the Trait keyword for the specific trait declaration, trait cannot instantiate

The following code example:

<?phpTrait dog{Public $name ="Dog";PublicfunctionBark(){Echo"This is the dog"; }}Classanimal{PublicfunctionEat () {echo  "This is animal Eat";} class cat extends animal{use dog; public function drive () {echo " this is Cat Drive "; }} $cat = new Cat (); $cat->drive ();  echo                 

The output will be as follows

Paste_image.png re-test trait, base class, and this class on the property or method with the same name, the following code
<?phpTrait dog{Public $name ="Dog";PublicfunctionDrive(){Echo"This is the dog drive"; }PublicfunctionEat(){Echo"This is the dog eat"; }}Classanimal{PublicfunctionDrive(){Echo"This was animal drive"; }Publicfunction eat() { echo "This is Animal Eat";}} class Cat extends animal{ use Dog; public function Drive() { echo ' This was cat drive ';}} $cat = New Cat (), $cat->drive ();  echo "<br/>"; $cat->eat ();? >                   

Shown below

Paste_image.png

Therefore: a method or property in trait overrides a method or property of the same name in the base class, and this class overrides a property or method with the same name in trait

A class can combine multiple trait, separated by commas, as follows

Use Trait1,trait2

When different trait have a method or property of the same name, there is a conflict, can be resolved with insteadof or as, Insteadof is substituted, and as is an alias
The following example:

<?phpTrait trait1{PublicfunctionEat(){Echo"This is trait1 eat"; }PublicfunctionDrive(){Echo"This was trait1 drive"; }}Trait trait2{PublicfunctionEat(){Echo"This is Trait2 eat"; }PublicfunctionDrive(){Echo"This was Trait2 drive"; }}Classcat{UseTrait1,trait2{TRAIT1::EatInsteadofTrait2; Trait1::d RiveInsteadof Trait2; }}Classdog{UseTrait1,trait2{TRAIT1::EatInsteadofTrait2; Trait1::d RiveInsteadof Trait2; Trait2::eatAs eaten; Trait2::d Rive as driven;}} $cat = New Cat (), $cat->eat ();  echo "<br/>"; $cat->drive ();  echo "<br/>";  echo "<br/>";  echo "<br/>"; $dog = new Dog (); $dog->eat ();  echo "<br/>"; $dog->drive ();  echo "<br/>"; $dog->eaten ();  echo "<br/>"; $dog->driven ();? >                  

The output is as follows

Paste_image.pngas can also modify the access control of the method
<?phpTrait animal{PublicfunctionEat(){Echo"This is Animal eat"; }}class dog{use animal{eat as protected;} class cat{ use animal{Animal::eat as private eaten;} $dog = new Dog (); $dog->eat (); new Cat (); $cat->eat (); //error, has been changed to a private access control ?>        
Trait can also be combined with each other, using abstract methods, static properties, static methods, and so on, for example:
<?phpTrait cat{PublicfunctionEat(){Echo"This is Cat eat"; }}Trait dog{UseCat;PublicfunctionDrive(){Echo"This is the Dog drive"; }AbstractPublicfunctionGetName();PublicfunctionTest(){static $num =0; $num + +;Echo $num; }PublicStaticfunctionSay(){Echo"This is the Dog say"; }}Classanimal{UseDog; public function getName() { echo ' This is animal name ';}} $animal = new Animal (); $animal->getname ();  echo "<br/>"; $animal->eat ();  echo "<br/>"; $animal->drive ();  echo "<br/>"; $animal:: Say ();  echo "<br/>"; $animal->test ();  echo "<br/>"; $animal->test ();? >                  

The output is as follows



Attachment know
Links: https://www.jianshu.com/p/fc053b2d7fd1
Source: Pinterest
The copyright of the book is owned by the author, and any form of reprint should be contacted by the author for authorization and attribution.

PHP7 's Trait detailed

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.