Home >Backend Development >PHP Tutorial >Detailed explanation of the difference between trait definition usage and interface in PHP

Detailed explanation of the difference between trait definition usage and interface in PHP

伊谢尔伦
伊谢尔伦Original
2017-07-03 11:12:072596browse

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. These problems are avoided after the emergence of Traits. The advantage of Traits is that they can be combined at will, and the code clear. This article mainly introduces traits in PHP. Friends in need can refer to it.

Preface

A former colleague changed jobs and was asked about PHP traits during the interview. I haven’t used it before, so I didn’t answer it well. I probably have used it a few times, so I thought about it and compiled the following summary.

trait

trait is a specific

attribute or method that some classes (Class) should have, while others with the same parent Classes should avoid using these attributes and methods . Of course, this is also related to the developer's ability to abstract the class. Some abstract abilities are good, which can reduce the need for traits. However, this situation should be unavoidable, otherwise the appearance of traits will be meaningless.

There is another situation, that is, when using traits, it can

constrain

developers Its function is to remind developers to pay attention to certain attributes and methods of traits that need to be called during the development process.

Colleagues raised a good question, doesn’t the interface also have this function?

Don’t worry, let’s look at an example first:

For example, if you want to collect various data on the website, you have developed the Spider class. Spider

has a method called

request() Responsible for requests.

<?php namespace XWSoul\Network;
class Spider
{
 public function request($url)
 {
 //do sth.
 }
}
However, in the process of collecting data, some websites are sensitive to spiders and some are not. For sensitive websites, we have given a solution using a proxy. However, using a proxy will affect the crawling speed. This leads to the situation that some subclasses of Spider need to use a proxy, and try not to use a proxy if possible.

So at this time we added a new trait Proxy :

<?php namespace XWSoul\Network;
trait Proxy
{

 protected $isProxy = false;

 public function useProxy($proxy)
 {
 //do sth proxy setups.
 $this->isProxy = true;
 return $this;
 }

 public function request($url)
 {
 if (!$this->isProxy) {
  throw new Exception("Please using proxy.");
 }
 //do sth.
 return parent::request($url);
 }
}

trait rewrites Spider's

request()

method, limiting that calling it without calling the proxy will

throw an exception .Back to the previous question, what is the difference between the usage of traits and interfaces?

The constraints of the interface are pre-defined and must be implemented at the beginning of the definition. He can Constraining the implementation of a method cannot constrain the calling of the method. A trait is a post-call. It has already implemented the method. The key is that it only constrains the class that calls itself (nonsense), but not the class that does not call itself. The class has no impact (another nonsense), and at the same time it is reusable, and does not destroy the implementation of the Spider class itself. Spider is still the same Spider.

Afterword

Someone may I decide to implement another request, for example, proxyRequst. Isn’t it over? What you said makes sense...but what if I use a different proxy and there are differences in the details of the request? I keep saying if if in the code. if? Trait Why should we give up such a refreshing solution?

The above is the detailed content of Detailed explanation of the difference between trait definition usage and interface in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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