Traits usage in PHP

Source: Internet
Author: User
Tags traits

PHP is a single-inheritance language, and PHP's class cannot inherit properties or methods from two base classes at the same time before PHP 5.4 traits appears. PHP's combination of traits and go language is a bit similar,

By using the Use keyword in a class to declare the name of the trait to be combined, the declaration of a specific trait uses trait keywords, trait cannot be instantiated directly. For specific usage, see the following code:

<?phptrait Drive { Public $carName= ' BMW ';  Public functiondriving () {Echo"Driving {$this->carname}\n "; }}classPerson { Public functionAge () {Echo"I am years old\n"; }}classStudentextendsPerson { UseDrive ;  Public functionStudy () {Echo"Learn to drive \ n"; }}$student=NewStudent ();$student-study ();$student-Age ();$student->driving ();
 - years olddriving BMW

In the above example, the student class inherits the person, has the age method, and, by combining drive, has the driving method and the property carname.

If a property or method of the same name exists in the trait, base class, and this class, which one will eventually be retained? Test it with the following code:

<?phptrait Drive { Public functionHello () {Echo"Hello Botong. \ n"; }     Public functiondriving () {Echo"Zhou Botong can't drive."; }}classPerson { Public functionHello () {Echo"Hello, everyone."; }     Public functiondriving () {Echo"Everybody's going to drive \"; }}classStudentextendsPerson { UseDrive;//The trait method overrides the method in the base class person, so the hello and driving in person are overwritten     Public functionHello () {Echo"Hello new student \ n";//When a method or property has the same name, the method in the current class overrides the Trait method, so here hello overwrites the hello in trait    }}$student=NewStudent ();$student-hello ();$student->driving ();
Hello new student Zhou Botong won't drive

It is therefore concluded that when a method or property has the same name, the methods in the current class override the Trait method, and the trait method overrides the method in the base class.

If you want to combine multiple trait, separate the trait name by commas:

Use Trait1, Trait2;

What happens if more than one trait contains a method or property of the same name? The answer is that when multiple trait of a combination contain a property or method with the same name, you need to explicitly declare a conflict resolution, or a fatal error will occur.

<?phptrait Trait1 { Public functionHello () {Echo"Trait1::hello\n"; }     Public functionhi () {Echo"Trait1::hi\n"; }}trait Trait2 { Public functionHello () {Echo"Trait2::hello\n"; }     Public functionhi () {Echo"Trait2::hi\n"; }}classClass1 { UseTrait1,Trait2;}
Output: Fatal error:trait method Hello have not been applied, because there is collisions with other Trait methods on Class 1 in

Using the insteadof and as operators to resolve conflicts, Insteadof uses a method instead of another, and as is an alias for the method, see the code for details:

<?phptrait Trait1 { Public functionHello () {Echo"Trait1::hello \ n"; }     Public functionhi () {Echo"Trait1::hi \ n"; }}trait Trait2 { Public functionHello () {Echo"Trait2::hello\n"; }     Public functionhi () {Echo"Trait2::hi\n"; }}classClass1 { UseTrait1,Trait2 {Trait2::Hello insteadof Trait1; Trait1::Hi insteadof Trait2; }}classClass2 { UseTrait1,Trait2 {Trait2::Hello insteadof Trait1; Trait1::Hi insteadof Trait2; Trait2:: Hi asHei; Trait1:: Hello ashehe; }}$OBJ 1=NewClass1 ();$OBJ 1-hello ();$OBJ 1-hi ();Echo"\ n";$OBJ 2=NewClass2 ();$OBJ 2-hello ();$OBJ 2-hi ();$OBJ 2-Hei ();$OBJ 2->hehe ();

As keyword there is another use, that is to modify 方法 the access control:

<?phptrait Hello { Public functionHello () {Echo"Hello, I'm botong."; }}classClass1 { UseHello {Hello as protected; }}classClass2 { UseHello {Hello:: Hello as PrivateHi; }}$OBJ 1=NewClass1 ();$OBJ 1->hello ();#because the Hello method is modified to a protected$OBJ 2=NewClass2 ();$OBJ 2->hello ();#output: Hello, I'm botong, because the original Hello method is still public$OBJ 2->hi ();#because the alias Hi method is modified to be private

" in D:\web\mytest\p.php:

Trait can also combine trait,trait to support abstract methods, static properties, and static methods, and test the code as follows:

<?phptrait Hello { Public functionSayHello () {Echo"Hello, I'm botong."; }}trait World { UseHello;  Public functionSayworld () {Echo"Hello world\n"; }    Abstract  Public functionGetworld ();  Public functionInc () {Static $c= 0; $c=$c+ 1; Echo"$c\ n "; }     Public Static functiondosomething () {Echo"Doing something\n"; }}classHelloWorld { UseWorld ;  Public functionGetworld () {return' Do you get the world? '; }}$OBJ=NewHelloWorld ();$OBJ-SayHello ();$OBJ-Sayworld ();Echo $OBJ->getworld (). "\ n"; HelloWorld::dosomething ();$OBJ-Inc ();$OBJ->inc ();
Hello i am botong thong Hello worlddo you get the world? Doing something12

Traits usage in PHP

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.