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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

PHPtraitDTO:简化数据传输对象的开发引言:在现代的软件开发中,数据传输对象(DataTransferObject,简称DTO)起到了重要的作用。DTO是一种纯粹的数据容器,用于在层与层之间传递数据。然而,在开发过程中,开发人员需要编写大量的相似的代码来定义和操作DTO。为了简化这一过程,PHP中引入了trait特性,我们可以利用trait特

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

深入了解PHPtraitDTO的设计模式与实践Introduction:在PHP开发中,设计模式是必不可少的一部分。其中,DTO(DataTransferObject)是一种常用的设计模式,用于封装数据传输的对象。而在实现DTO的过程中,使用trait(特征)可以有效地提高代码的复用性和灵活性。本文将深入探讨PHP中traitDTO的设计模式与实践

去除方法:1、使用substr_replace()函数将首位数字替换为空字符串即可,语法“substr_replace($num,"",0,1)”;2、用substr截取从第二位数字开始的全部字符即可,语法“substr($num,1)”。

PHPtraitDTO:实现数据传输对象的简洁性与灵活性引言:在PHP开发过程中,经常会涉及到数据的传输与处理。而传输对象模式(DataTransferObject,简称DTO)是一种设计模式,它用于将数据在不同层之间传输。在传输过程中,DTO通过封装数据、提供公共访问方法来简化数据的操作。本文将介绍如何使用PHPtrait来实现DT

php去掉数组键值的方法:1、使用“array_keys($array)”语句,可去掉全部键值,返回包含全部键名的数组;2、使用“array_splice($array,$start,$length)”语句,可去掉指定位置的一个或多个键值。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver CS6
Visual web development tools
