Home  >  Article  >  Backend Development  >  A brief introduction to traits in PHP

A brief introduction to traits in PHP

墨辰丷
墨辰丷Original
2018-05-25 09:54:221192browse

Since PHP5.4.0, PHP has implemented a code reuse method called Trait. Because PHP is single inheritance, when you need multiple different features, you have to combine multiple base classes. The emergence of Trait avoids these problems. The advantage of Trait is that it can be combined at will and the code is 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, 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.
 }
}

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);
 }
}

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 using different proxies? Do I keep saying if if if in the code? Why should we give up such a refreshing solution like trait?

The above is the entire content of this article , I hope it will be helpful to everyone’s study.


Related recommendations:

traitUsage method in PHP with pictures and text

Traits attributes and basic usage of php

Sharing the characteristics and functions of Traits in PHP

The above is the detailed content of A brief introduction to traits 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