Home> headlines> body text

Detailed explanation and application of Trait in php

小云云
Release: 2017-11-04 09:40:28
Original
1748 people have browsed it

1. Inheritance VS Polymorphism VS Trait

There are now two classes: Publish.php and Answer.php. To add LOG function to it, record the actions inside the class. There are several options:

Inherit polymorphic Trait 1.1. Inherit

as shown in the figure:

Detailed explanation and application of Trait in php

code The structure is as follows:

// Log.php

You can see that inheritance indeed meets the requirements. But this violates the object-oriented principle. The relationship between operations such as Publish and Answer and Log is not the relationship between the subclass and the parent class. So it is not recommended to use it this way.

1.2. Polymorphism

As shown:

Detailed explanation and application of Trait in php

Implementation code:

// Log.php

The logging operations should be the same, so the logging implementation in the Publish and Answer actions is also the same. Obviously, this violates the DRY (Don’t Repeat Yourself) principle. Therefore, it is not recommended to implement it this way.

1.3. Trait

As shown:

Detailed explanation and application of Trait in php

The implementation code is as follows:

// Log.php startLog(); $publish->endLog(); // Answer.php startLog(); $answer->endLog();

As you can see, we have achieved the code without increasing the complexity of the code. Reuse.

1.4. Conclusion

Although the inheritance method can also solve the problem, its idea violates the object-oriented principle and seems very crude; the polymorphic method is also feasible, but it is not in line with the software development process. The DRY principle increases maintenance costs. The Trait method avoids the above shortcomings and achieves code reuse relatively elegantly.

2. The scope of Trait

After understanding the benefits of Trait, we also need to understand the rules in its implementation. First, let’s talk about scope. This is easier to prove. The implementation code is as follows:

publicF(); $this->protectF(); $ this->privateF(); } } $publish = new Publish(); $publish->doPublish();

The output result of executing the above code is as follows:

public function protected function private function

It can be found that the scope of Trait is visible inside the Trait class that references it. It can be understood that the use keyword copies the implementation code of the Trait into the class that references the Trait.

3. Priority of attributes in Trait

When it comes to priority, there must be a reference object for comparison. The reference object here refers to the Trait class and its parent class.

Use the following code to prove the priority of attributes in the Trait application:

publicF(); $this- >protectF(); } } $publish = new Publish(); $publish->doPublish();

The output of the above code is as follows:

Publish::publicF public function Log::protectF protected function

Through the above example, the priority in Trait application can be summarized as follows:

Members from the current class override the trait's method and the trait overrides the inherited one. Method

The priority of class members is: Current class>Trait>Parent class

4. Insteadof and As keywords

In one class, multiple Traits can be referenced , as follows:

startLog(); $this-> parameterCheck($para); $this->endLog(); } }

Through the above method, we can reference multiple Traits in a class. When referencing multiple Traits, it is easy to cause problems. The most common problem is what to do if there are properties or methods with the same name in two Traits? At this time, you need to use the keywords Insteadof and as .Please see the following implementation code:

startLog(); $this->parameterCheck('params'); $this->csl(); } } $publish = new Publish(); $publish->doPublish();

Execute the above code, the output result is as follows:

Log: :startLog public function Check::parameterCheck parameter checkparams Check::startLog public function

Just like the literal meaning, the insteadof keyword replaces the latter with the former, and the as keyword gives an alias to the replaced method. .

When referencing Trait, the use keyword is used, and the use keyword is also used to refer to the namespace. The difference between the two is that when referencing Trait, it is used inside the class.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!