Traits in PHP

高洛峰
Release: 2023-03-06 08:38:01
Original
1469 people have browsed 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, and other classes with the same parent should avoid including these attributes. Used in the case of methods.

Of course, this is also related to the developer's ability to abstract classes. Some people with good abstraction capabilities can reduce the use of traits, but this situation should be unavoidable otherwise traits It is meaningless if it appears.

There is another situation, that is, when using traits, it can play a role in constraining developers, reminding developers to pay attention to certain attributes of traits that need to be called during the development process. and methods.

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

No hurry, 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 has a method called request() is responsible for the request.

<?php namespace XWSoul\Network;
class Spider
{
 public function request($url)
 {
 //do sth.
 }
}
Copy after login

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 Crawl speed. This leads to the situation that some subclasses of Spider need to use proxies, but try not to use proxies 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);
 }
}
Copy after login

trait rewrites Spider's request() method, limiting that an exception will be thrown when called without calling the proxy.

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 it is reusable, and does not destroy the implementation of the Spider class itself. Spider is still the same Spider.

I think the usage of trait is already very effective here. Alright.

Afterword

Someone may decide to implement another request, for example, proxyRequst. Wouldn’t that be the end of it? What you said makes sense...but if I What should I do if there are differences in the details of requests when using different proxies? Do I keep saying if if if in the code? Trait Why should we give up such a refreshing solution?

More traits in PHP For related articles, please pay attention to the PHP Chinese website!


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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template