Since PHP5.4.0, PHP has implemented a code reuse method called Trait. Because PHP is a single inheritance, when you need multiple different features, you have to combine multiple base classes. when Trait appears, these problems are avoided, the advantage of Trait is that it can be combined freely and the code is clear. This article mainly introduces trait in PHP. For more information, see.
Preface
A former colleague asked for PHP trait during the interview. I didn't answer the question because I have never used it. I tried it several times and thought about the following summary.
Trait
Trait is a specific attribute or method that some classes should possess. other classes at the same parent level should not be used when these attributes and methods are included.
Of course, this is also related to the developer's ability to abstract classes. some of them have good abstract capabilities and can reduce the use of trait. However, this situation should be unavoidable. otherwise, trait will be meaningless.
Another scenario is to use trait to constrain developers and remind developers to call certain attributes and methods of trait during development.
My colleague raised a good question. isn't interface also used?
Don't worry. let's look at an example first:
For example, if you want to collect various types of data on the website, you have developed the Spider class.. Spider There is a method called request() Request.
However, some websites are not sensitive to spider data. for sensitive websites, we provide a proxy solution. however, using a proxy will affect the crawling speed. this results in some cases where the Spider subclass needs to be used as a proxy, and the Spider subclass does not need to be used as much as possible.
So we added a trait Proxy:
isProxy = true; return $this; } public function request($url) { if (!$this->isProxy) { throw new Exception("Please using proxy."); } //do sth. return parent::request($url); }}Trait overwrites the Spider'srequest() Method, which means that an exception is thrown when no proxy is called.
Back to the previous question, what is the difference between trait usage and interface?
Interface constraints are pre-defined and must be implemented at the beginning. They can constrain the implementation of methods but cannot constrain the call of methods. trait is a post-called method, he has implemented the method. The key is that he only imposes constraints on the class called (nonsense ), it does not affect the class that does not call itself (another nonsense). at the same time, it is reusable and does not damage the implementation of the Spider class. the Spider is still the Spider.
I think the usage of trait is quite effective here.
Remarks
Someone may decide to implement another request, for example, proxyRequst. isn't that all done? What you said makes sense... However, what if I use a different proxy that varies with the request details? Does the code keep if? Why should trait give up on such a refreshing solution?
For more articles about trait in PHP, refer to PHP Chinese network!