This article will introduce to you how to use the trait capability in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
I believe that everyone is no longer unfamiliar with traits. As early as 5.4, traits have already appeared in the new features of PHP. Of course, trait itself also means feature, but the main capability of this feature is for code reuse.
We all know that PHP is a modern object-oriented language. In order to solve the confusing problem of multiple inheritance in C, most languages are in the form of single inheritance and multiple interfaces, but this will also make some reusable code must be implemented through combination. If combination is to be used, it is inevitable To instantiate a class or use a static method, the memory usage is increased. In order to solve this problem, PHP officially launched the trait capability. You can think of it as a variation of combo abilities.
trait A { public $a = 'A'; public function testA() { echo 'This is ' . $this->a; } } class classA { use A; } class classB { use A; public function __construct() { $this->a = 'B'; } } $a = new classA(); $b = new classB(); $a->testA(); $b->testA();
From the above code, we can see that trait can be applied to any class, and variables can be defined, which is very convenient. The most important thing to note about traits is the overload priority of methods with the same name.
trait B { function test(){ echo 'This is trait B!'; } } trait C { function test(){ echo 'This is trait C!'; } } class testB{ use B, C; function test(){ echo 'This is class testB!'; } } $b = new testB(); $b->test(); // This is class testB! // class testC{ // use B, C; // } // $c = new testC(); // $c->test(); // Fatal error: Trait method test has not been applied, because there are collisions with other trait methods on testC
Here, the test() method is overloaded in our class, and the output here is the method in the class. If you comment out the test() method in the testB class, an error will be reported. Because the program cannot distinguish which trait's test() method you want to use. We can use insteadof to specify which trait the method to use is called.
class testE{ use B, C { B::test insteadOf C; C::test as testC; } } $e = new testE(); $e->test(); // This is trait B! $e->testC(); // This is trait C!
Of course, in actual development, we should try to standardize method names to avoid such duplication. In addition, what if the subclass references a trait and the parent class defines the same method? Of course, the methods inherited from the parent class are still called. The priority of traits is lower than that of ordinary class inheritance.
trait D{ function test(){ echo 'This is trait D!'; } } class parentD{ function test(){ echo 'This is class parentD'; } } class testD extends parentD{ use D; } $d = new testD(); $d->test(); // This is trait D!
Finally, abstract methods can also be defined in traits. This abstract method is a method that must be implemented by the class that references this trait, and has the same effect as the abstract method in the abstract class.
trait F{ function test(){ echo 'This is trait F!'; } abstract function show(); } class testF{ use F; function show(){ echo 'This is class testF!'; } } $f = new testF(); $f->test(); $f->show();
Trait is really that PHP is a very flexible function. Of course, the more flexible something is, the more we need to figure out some of its usage rules, so as to avoid some unforeseen errors.
Test code:
https://github.com/zhangyue0503/dev-blog/blob/master/php/201912/source/trait%E8%83%BD%E5%8A%9B%E5%9C%A8PHP%E4%B8%AD%E7%9A%84%E4%BD%BF%E7%94%A8.php
Recommended learning: php video tutorial
The above is the detailed content of How to use trait capabilities 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”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

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

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
