PHP 实现了一种代码复用的方法,称为 trait,复用trait
自 PHP 5.4.0 起,PHP 实现了一种代码复用的方法,称为 trait。
Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制。Trait 为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用 method。
Trait 是 PHP 多重继承的一种解决方案。例如,需要同时继承两个 Abstract Class, 这将会是件很麻烦的事情,Trait 就是为了解决这个问题。
它为传统继承增加了水平特性的组合。
例子1: 使用trait关键字定义trait
<span>trait first_trait{ </span><span>public</span> <span>function</span><span> hello(){ </span><span>return</span> 'hello'<span>; } }</span>
例子2: 在Class里使用trait,要使用use关键字,使用多个trait时用英文逗号隔开
<span>trait first_trait{ </span><span>public</span> <span>function</span><span> hello(){ </span><span>return</span> 'hello'<span>; } } trait second_trait{ </span><span>public</span> <span>function</span><span> world(){ </span><span>return</span> 'world'<span>; } } </span><span>class</span><span> first_class{ </span><span>use</span> first_trait,<span>second_trait; } </span><span>$obj</span>=<span>new</span><span> first_class(); </span><span>echo</span> <span>$obj</span>-><span>hello(); </span><span>echo</span> <span>$obj</span>->world();
例子3: 优先级
从基类继承的成员会被 trait 插入的成员所覆盖。优先顺序是来自当前类的成员覆盖了 trait 的方法,而 trait 则覆盖了被继承的方法。
例子:从基类继承的成员会被 trait 插入的成员所覆盖
<span>class</span><span> Base { </span><span>public</span> <span>function</span><span> sayHello() { </span><span>echo</span> 'Hello '<span>; } } trait SayWorld { </span><span>public</span> <span>function</span><span> sayHello() { parent</span>::<span>sayHello(); </span><span>echo</span> 'World!'<span>; } } </span><span>class</span> MyHelloWorld <span>extends</span><span> Base { </span><span>use</span><span> SayWorld; } </span><span>$o</span> = <span>new</span><span> MyHelloWorld(); </span><span>$o</span>-><span>sayHello(); </span><span>//</span><span>输出的结果</span> Hello World!
例子:当前类的成员覆盖了 trait 的方法
<span>trait HelloWorld { </span><span>public</span> <span>function</span><span> sayHello() { </span><span>echo</span> 'Hello World!'<span>; } } </span><span>class</span><span> TheWorldIsNotEnough { </span><span>use</span><span> HelloWorld; </span><span>public</span> <span>function</span><span> sayHello() { </span><span>echo</span> 'Hello Universe!'<span>; } } </span><span>$o</span> = <span>new</span><span> TheWorldIsNotEnough(); </span><span>$o</span>-><span>sayHello(); </span><span>//</span><span>输出的结果</span> Hello Universe!
例子4: trait之间的嵌套
<span>trait first_trait{ </span><span>public</span> <span>function</span><span> hello(){ </span><span>echo</span> 'hello'<span>; } } trait second_trait{ </span><span>//</span><span>trait之间的嵌套</span> <span>use</span><span> first_trait; </span><span>public</span> <span>function</span><span> world(){ </span><span>echo</span> 'world'<span>; } } </span><span>class</span><span> first_class{ </span><span>use</span><span> second_trait; } </span><span>$obj</span>=<span>new</span><span> first_class(); </span><span>echo</span> <span>$obj</span>-><span>hello(); </span><span>echo</span> <span>$obj</span>->world();
例子5: 可以在trait中声明抽象方法,使用它的Class或trait必须实现抽象方法
<span>trait first_trait{ </span><span>public</span> <span>function</span><span> hello(){ </span><span>echo</span> 'hello'<span>; } </span><span>//</span><span>抽象方法</span> <span>public</span> <span>abstract</span> <span>function</span><span> test(); } trait second_trait{ </span><span>//</span><span>trait之间的嵌套</span> <span>use</span><span> first_trait; </span><span>public</span> <span>function</span><span> world(){ </span><span>echo</span> 'world'<span>; } </span><span>//</span><span>实现first_trait 中的test方法</span> <span>public</span> <span>function</span><span> test(){ </span><span>echo</span> '!'<span>; } } </span><span>class</span><span> first_class{ </span><span>use</span><span> second_trait; } </span><span>$obj</span>=<span>new</span><span> first_class(); </span><span>echo</span> <span>$obj</span>-><span>hello(); </span><span>echo</span> <span>$obj</span>-><span>world(); </span><span>echo</span> <span>$obj</span>-><span>test(); </span><span>//</span><span>会输出</span> helloworld!
例子6: 冲突的解决
如果两个 trait 都插入了一个同名的方法,如果没有明确解决冲突将会产生一个致命错误。
为了解决多个 trait 在同一个类中的命名冲突,需要使用 insteadof 操作符来明确指定使用冲突方法中的哪一个。
以上方式仅允许排除掉其它方法,as 操作符可以将其中一个冲突的方法以另一个名称来引入,相当于方法的别名。
<span>trait A { </span><span>public</span> <span>function</span><span> smallTalk() { </span><span>echo</span> 'a'<span>; } </span><span>public</span> <span>function</span><span> bigTalk() { </span><span>echo</span> 'A'<span>; } } trait B { </span><span>public</span> <span>function</span><span> smallTalk() { </span><span>echo</span> 'b'<span>; } </span><span>public</span> <span>function</span><span> bigTalk() { </span><span>echo</span> 'B'<span>; } } </span><span>class</span><span> Talker { </span><span>use</span> A,<span> B { B</span>::smallTalk insteadof A; <span>//</span><span>trait B 的smallTalk方法会代替 trait A 的smallTalk方法</span> A::bigTalk insteadof B; <span>//</span><span>trait A 的bigTalk方法会代替 trait B 的bigTalk方法</span> <span> } } </span><span>class</span><span> Aliased_Talker { </span><span>use</span> A,<span> B { B</span>::smallTalk insteadof A;<span>//</span><span>trait B 的smallTalk方法会代替 trait A 的smallTalk方法</span> A::bigTalk insteadof B;<span>//</span><span>trait A 的bigTalk方法会代替 trait B 的bigTalk方法</span> B::bigTalk <span>as</span> talk; <span>//</span><span>使用 as 操作符来定义了 talk方法 来作为 B 的 bigTalk方法 的别名</span> <span> } } </span><span>$obj</span>=<span>new</span><span> Talker(); </span><span>$obj</span>-><span>smallTalk(); </span><span>$obj</span>-><span>bigTalk(); </span><span>//</span><span>结果会输出 bA</span> <span>$obj2</span>=<span>new</span><span> Aliased_Talker(); </span><span>$obj2</span>->talk();<span>//</span><span>会输出B</span>
例子7: 修改方法的访问控制
<span>trait HelloWorld { </span><span>public</span> <span>function</span><span> sayHello() { </span><span>echo</span> 'Hello World!'<span>; } } </span><span>//</span><span> 修改 sayHello 的访问控制</span> <span>class</span><span> MyClass1 { </span><span>use</span> HelloWorld { sayHello <span>as</span> <span>protected</span><span>; } } </span><span>//</span><span> 给方法一个改变了访问控制的别名 // 原版 sayHello 的访问控制则没有发生变化</span> <span>class</span><span> MyClass2 { </span><span>use</span> HelloWorld { sayHello <span>as</span> <span>private</span><span> myPrivateHello; } }</span>
例子8: Trait 同样可以定义属性
<span>trait PropertiesTrait { </span><span>public</span> <span>$x</span> = 1<span>; } </span><span>class</span><span> PropertiesExample { </span><span>use</span><span> PropertiesTrait; } </span><span>$example</span> = <span>new</span><span> PropertiesExample; </span><span>$example</span>->x;
如果 trait 定义了一个属性,那类将不能定义同样名称的属性,否则会产生一个错误。如果该属性在类中的定义与在 trait 中的定义兼容(同样的可见性和初始值)则错误的级别是 E_STRICT
,否则是一个致命错误。
<span>trait PropertiesTrait { </span><span>public</span> <span>$same</span> = <span>true</span><span>; </span><span>public</span> <span>$different</span> = <span>false</span><span>; } </span><span>class</span><span> PropertiesExample { </span><span>use</span><span> PropertiesTrait; </span><span>public</span> <span>$same</span> = <span>true</span>; <span>//</span><span> Strict Standards</span> <span>public</span> <span>$different</span> = <span>true</span>; <span>//</span><span> 致命错误</span> }
如果您阅读过此文章有所收获,请为我顶一个,如果文章中有错误的地方,欢迎指出。

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

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

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

在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

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment
