Home > Article > Backend Development > Learning PHP--New Traits
在阅读yii2源码的时候接触到了trait,就学习了一下,写下博客记录一下。
自 PHP 5.4.0 起,PHP 实现了代码复用的一个方法,称为 traits。
Traits 是一种为类似 PHP 的单继承语言而准备的代码复用机制。Trait 为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用方法集。Traits 和类组合的语义是定义了一种方式来减少复杂性,避免传统多继承和混入类(Mixin)相关的典型问题。
Trait 和一个类相似,但仅仅旨在用细粒度和一致的方式来组合功能。Trait 不能通过它自身来实例化。它为传统继承增加了水平特性的组合;也就是说,应用类的成员不需要继承。
Trait 示例
<?<span>php trait ezcReflectionReturnInfo { </span><span>function</span> getReturnType() { <span>/*</span><span>1</span><span>*/</span><span> } </span><span>function</span> getReturnDescription() { <span>/*</span><span>2</span><span>*/</span><span> } } </span><span>class</span> ezcReflectionMethod <span>extends</span><span> ReflectionMethod { </span><span>use</span><span> ezcReflectionReturnInfo; </span><span>/*</span><span> ... </span><span>*/</span><span> } </span><span>class</span> ezcReflectionFunction <span>extends</span><span> ReflectionFunction { </span><span>use</span><span> ezcReflectionReturnInfo; </span><span>/*</span><span> ... </span><span>*/</span><span> } </span>?>
优先级
从基类继承的成员被 trait 插入的成员所覆盖。优先顺序是来自当前类的成员覆盖了 trait 的方法,而 trait 则覆盖了被继承的方法。
优先顺序示例
<?<span>php </span><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>?>
以上例程会输出:Hello World!
从基类继承的成员被插入的 SayWorld Trait 中的 sayHello 方法所覆盖。其行为 MyHelloWorld 类中定义的方法一致。优先顺序是当前类中的方法会覆盖 trait 方法,而 trait 方法又覆盖了基类中的方法。
另一个优先级顺序的例子
<?<span>php 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>?>
以上例程会输出:Hello Universe!
多个 trait
通过逗号分隔,在 use 声明列出多个 trait,可以都插入到一个类中。
多个 trait 的用法的例子
<?<span>php trait Hello { </span><span>public</span> <span>function</span><span> sayHello() { </span><span>echo</span> 'Hello '<span>; } } trait World { </span><span>public</span> <span>function</span><span> sayWorld() { </span><span>echo</span> 'World'<span>; } } </span><span>class</span><span> MyHelloWorld { </span><span>use</span> Hello,<span> World; </span><span>public</span> <span>function</span><span> sayExclamationMark() { </span><span>echo</span> '!'<span>; } } </span><span>$o</span> = <span>new</span><span> MyHelloWorld(); </span><span>$o</span>-><span>sayHello(); </span><span>$o</span>-><span>sayWorld(); </span><span>$o</span>-><span>sayExclamationMark(); </span>?>
以上例程会输出:Hello World!
冲突的解决
如果两个 trait 都插入了一个同名的方法,如果没有明确解决冲突将会产生一个致命错误。
为了解决多个 trait 在同一个类中的命名冲突,需要使用 insteadof 操作符来明确指定使用冲突方法中的哪一个。
以上方式仅允许排除掉其它方法,as 操作符可以将其中一个冲突的方法以另一个名称来引入。
冲突解决的例子
<?<span>php 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>::<span>smallTalk insteadof A; A</span>::<span>bigTalk insteadof B; } } </span><span>class</span><span> Aliased_Talker { </span><span>use</span> A,<span> B { B</span>::<span>smallTalk insteadof A; A</span>::<span>bigTalk insteadof B; B</span>::bigTalk <span>as</span><span> talk; } } </span>?>
在本例中 Talker 使用了 trait A 和 B。由于 A 和 B 有冲突的方法,其定义了使用 trait B 中的 smallTalk 以及 trait A 中的 bigTalk。
Aliased_Talker 使用了 as 操作符来定义了 talk 来作为 B 的 bigTalk 的别名。
修改方法的访问控制
使用 as 语法还可以用来调整方法的访问控制。
修改方法的访问控制的例子
<?<span>php 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>?>
从 trait 来组成 trait
正如类能够使用 trait 一样,其它 trait 也能够使用 trait。在 trait 定义时通过使用一个或多个 trait,它能够组合其它 trait 中的部分或全部成员。
从 trait 来组成 trait的例子
<?<span>php trait Hello { </span><span>public</span> <span>function</span><span> sayHello() { </span><span>echo</span> 'Hello '<span>; } } trait World { </span><span>public</span> <span>function</span><span> sayWorld() { </span><span>echo</span> 'World!'<span>; } } trait HelloWorld { </span><span>use</span> Hello,<span> World; } </span><span>class</span><span> MyHelloWorld { </span><span>use</span><span> HelloWorld; } </span><span>$o</span> = <span>new</span><span> MyHelloWorld(); </span><span>$o</span>-><span>sayHello(); </span><span>$o</span>-><span>sayWorld(); </span>?>
以上例程会输出:Hello World!
Trait 的抽象成员
为了对使用的类施加强制要求,trait 支持抽象方法的使用。
表示通过抽象方法来进行强制要求的例子
<?<span>php trait Hello { </span><span>public</span> <span>function</span><span> sayHelloWorld() { </span><span>echo</span> 'Hello'.<span>$this</span>-><span>getWorld(); } </span><span>abstract</span> <span>public</span> <span>function</span><span> getWorld(); } </span><span>class</span><span> MyHelloWorld { </span><span>private</span> <span>$world</span><span>; </span><span>use</span><span> Hello; </span><span>public</span> <span>function</span><span> getWorld() { </span><span>return</span> <span>$this</span>-><span>world; } </span><span>public</span> <span>function</span> setWorld(<span>$val</span><span>) { </span><span>$this</span>->world = <span>$val</span><span>; } } </span>?>
Trait 的静态成员
Traits 可以被静态成员静态方法定义。
静态变量的例子
<?<span>php trait Counter { </span><span>public</span> <span>function</span><span> inc() { </span><span>static</span> <span>$c</span> = 0<span>; </span><span>$c</span> = <span>$c</span> + 1<span>; </span><span>echo</span> "<span>$c</span>\n"<span>; } } </span><span>class</span><span> C1 { </span><span>use</span><span> Counter; } </span><span>class</span><span> C2 { </span><span>use</span><span> Counter; } </span><span>$o</span> = <span>new</span> C1(); <span>$o</span>->inc(); <span>//</span><span> echo 1</span> <span>$p</span> = <span>new</span> C2(); <span>$p</span>->inc(); <span>//</span><span> echo 1</span> ?>
静态方法的例子
<?<span>php trait StaticExample { </span><span>public</span> <span>static</span> <span>function</span><span> doSomething() { </span><span>return</span> 'Doing something'<span>; } } </span><span>class</span><span> Example { </span><span>use</span><span> StaticExample; } Example</span>::<span>doSomething(); </span>?>
静态变量和静态方法的例子
<?<span>php trait Counter { </span><span>public</span> <span>static</span> <span>$c</span> = 0<span>; </span><span>public</span> <span>static</span> <span>function</span><span> inc() { self</span>::<span>$c</span> = self::<span>$c</span> + 1<span>; </span><span>echo</span> self::<span>$c</span> . "\n"<span>; } } </span><span>class</span><span> C1 { </span><span>use</span><span> Counter; } </span><span>class</span><span> C2 { </span><span>use</span><span> Counter; } C1</span>::inc(); <span>//</span><span> echo 1</span> C2::inc(); <span>//</span><span> echo 1</span> ?>
属性
Trait 同样可以定义属性。
定义属性的例子
<?<span>php 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>-><span>x; </span>?>
如果 trait 定义了一个属性,那类将不能定义同样名称的属性,否则会产生一个错误。如果该属性在类中的定义与在 trait 中的定义兼容(同样的可见性和初始值)则错误的级别是 E_STRICT
,否则是一个致命错误。
冲突的例子
<?<span>php 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> <span>} </span>?>
Use的不同
不同use的例子
<?<span>php namespace Foo\Bar; </span><span>use</span> Foo\Test; <span>//</span><span> means \Foo\Test - the initial \ is optional</span> ?> <?<span>php namespace Foo\Bar; </span><span>class</span><span> SomeClass { </span><span>use</span> Foo\Test; <span>//</span><span> means \Foo\Bar\Foo\Test</span> <span>} </span>?>
第一个use是用于 namespace 的 use Foo\Test,找到的是 \Foo\Test,第二个 use 是使用一个trait,找到的是\Foo\Bar\Foo\Test。
__CLASS__和__TRAIT__
__CLASS__ 返回 use trait 的 class name,__TRAIT__返回 trait name
示例如下
<?<span>php trait TestTrait { </span><span>public</span> <span>function</span><span> testMethod() { </span><span>echo</span> "Class: " . <span>__CLASS__</span> . <span>PHP_EOL</span><span>; </span><span>echo</span> "Trait: " . __TRAIT__ . <span>PHP_EOL</span><span>; } } </span><span>class</span><span> BaseClass { </span><span>use</span><span> TestTrait; } </span><span>class</span> TestClass <span>extends</span><span> BaseClass { } </span><span>$t</span> = <span>new</span><span> TestClass(); </span><span>$t</span>-><span>testMethod(); </span><span>//</span><span>Class: BaseClass //Trait: TestTrait</span>
Trait单例
实例如下
<?<span>php trait singleton { </span><span>/*</span><span>* * private construct, generally defined by using class </span><span>*/</span> <span>//</span><span>private function __construct() {}</span> <span>public</span> <span>static</span> <span>function</span><span> getInstance() { </span><span>static</span> <span>$_instance</span> = <span>NULL</span><span>; </span><span>$class</span> = <span>__CLASS__</span><span>; </span><span>return</span> <span>$_instance</span> ?: <span>$_instance</span> = <span>new</span> <span>$class</span><span>; } </span><span>public</span> <span>function</span><span> __clone() { </span><span>trigger_error</span>('Cloning '.<span>__CLASS__</span>.' is not allowed.',<span>E_USER_ERROR</span><span>); } </span><span>public</span> <span>function</span><span> __wakeup() { </span><span>trigger_error</span>('Unserializing '.<span>__CLASS__</span>.' is not allowed.',<span>E_USER_ERROR</span><span>); } } </span><span>/*</span><span>* * Example Usage </span><span>*/</span> <span>class</span><span> foo { </span><span>use</span><span> singleton; </span><span>private</span> <span>function</span><span> __construct() { </span><span>$this</span>->name = 'foo'<span>; } } </span><span>class</span><span> bar { </span><span>use</span><span> singleton; </span><span>private</span> <span>function</span><span> __construct() { </span><span>$this</span>->name = 'bar'<span>; } } </span><span>$foo</span> = foo::<span>getInstance(); </span><span>echo</span> <span>$foo</span>-><span>name; </span><span>$bar</span> = bar::<span>getInstance(); </span><span>echo</span> <span>$bar</span>->name;
调用trait方法
虽然不很明显,但是如果Trait的方法可以被定义为在普通类的静态方法,就可以被调用
实例如下
<?<span>php trait Foo { </span><span>function</span><span> bar() { </span><span>return</span> 'baz'<span>; } } </span><span>echo</span> Foo::bar(),"\\n"<span>; </span>?>
以上就介绍了PHP的学习--Traits新特性,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。