您的位置 首页 php

PHP中实现代码复用的Trait方法的一些特性

Trait 是 PHP5.4 中的新特性,是 PHP 多重继承 的一种解决方案。例如,需要同时继承两个 Abstract Class, 这将会是件很麻烦的事情,Trait 就是为了解决这个问题。

trait 的简单使用

trait使用前需要先定义,trait的定义方式和类的定义方式差不多

trait first_trait {

function first_method() { /* Code Here */ }

function second_method() { /* Code Here */ }

}

同时,如果要在 Class 中使用该 Trait,那么需要使用 use 关键字

class first_class {

// 注意这行,声明使用 first_trait

use first_trait;

}

$obj = new first_class();// Executing the method from trait

$obj->first_method(); // valid

$obj->second_method(); // valid

我们在类中可以直接声明使用被定义好的trait,之后在类被 实例化 后,就可以使用调用对象中方法的方式直接调用trait中的方法。无疑这增加了很多扩展性,因为类的继承是单一的,而trait的使用却不是单一的。

一个 class 中使用多个 trait

在同一个 Class 中可以使用多个 Trait,多个 Trait之间通过逗号分隔。

例如:

trait first_trait

{

function first_method() { echo “method1”; }

}

trait second_trait {

function second_method() { echo “method2”; }

}

class first_class {

// now using more than one trait

use first_trait, second_trait;

}

$obj= new first_class();

// Valid

$obj->first_method(); // Print : method1

// Valid

$obj->second_method(); // Print : method2

Trait 冲突

多个 Trait 之间同时使用难免会冲突,例如,如果两个 trait 都插入了一个同名的方法,如果没有明确解决冲突将会产生一个致命错误。

为了解决多个 trait 在同一个类中的命名冲突,可以使用关键字:insteadof 以及 as ;

insteadof 操作符用来明确指定使用哪一个冲突方法;

as 操作符用来给冲突的方法重新命名并引入。

trait first_trait {

function first_function() {

echo “From First Trait”;

}

}

trait second_trait {

// 这里的名称和 first_trait 一样,会有冲突

function first_function() {

echo “From Second Trait”;

}

}

class first_class {

use first_trait, second_trait {

// 在这里声明使用 first_trait 的 first_function 替换

// second_trait 中声明的

first_trait::first_function insteadof second_trait;

second_trait::first_function as second_trait;

}

}

Trait 的抽象方法

我们可以在Trait 中声明抽象方法,这样,使用它的 Class 必须实现它。

trait first_trait {

function first_method() { echo “method1”; }

abstract public function second_method();// 这里可以加入修饰符,说明调用类必须实现它

}

class first_method {

use first_trait;

function second_method() {

/* Code Here */

}

}

Trait 优先级

PHP手册说法:优先顺序是当前类的方法覆盖了 trait 的方法,而 trait 覆盖了被继承的方法。

即优先级:当前类方法 > trait方法 > 被继承的方法。

如果在不同优先级中定义了一个同名的方法,那么优先级高的方法将覆盖优先级低的方法;

两个trait属于同一优先级,若出现同名方法,则会报致命错误,具体参见上面。

class Base {

public function sayHello() {

echo ‘Hello ‘;

}

}

trait SayWorld {

public function sayHello() {

parent::sayHello();

echo ‘World!’;

}

}

class My HelloWorld extends Base {

use SayWorld;

}

$o = new MyHelloWorld();

$o->sayHello();

结果会输出:

Hello World!

Trait 访问权限控制

与类相似的,我们在定义trait的方法时,也可以指定其访问权限(public、protected、private);

我们还可以使用as来修改方法的访问权限。

trait HelloWorld {

public function sayHello() {

echo ‘Hello World!’;

}

}

// 修改 sayHello 的 访问控制

class MyClass1 {

use HelloWorld { sayHello as protected; }

}

// 给方法一个改变了访问控制的别名

// 原版 sayHello 的访问控制则没有发生变化

class MyClass2 {

use HelloWorld { sayHello as private myPrivateHello; }

}

Trait 之间的嵌套

使用trait时,可以将不同的trait组成新的trait,这样使得代码的复用率变得更高,当然对程序的设计要求也更高。

trait Hello {

public function sayHello() {

echo ‘Hello ‘;

}}

trait World {

public function sayWorld() {

echo ‘World!’;

}}

trait HelloWorld {

use Hello, World;

}

class MyHelloWorld {

use HelloWorld;

}

$o = new MyHelloWorld();

$o->sayHello();

$o->sayWorld();

//输出 Hello World!

Trait 的静态成员

与类相似,静态成员分静态方法和静态属性,二者都是以 static 关键字来进行声明的。

trait Counter {

public function inc() {

static $c = 0;

$c = $c + 1;

echo “$c\n”;

}

}

trait StaticExample {

public static function doSomething() {

return ‘Doing something’;

}

}

Trait 属性

在trait中定义了属性后,类的实例可以像访问自身的属性一样访问它。而且可以给它设置访问权限,就差不能实例化了……

需要注意的是,如果 trait 定义了一个属性,那类将不能定义同样名称的属性,否则会产生一个错误。如果该属性在类中的定义与在 trait 中的定义兼容(同样的可见性和初始值)则错误的级别是 E_STRICT,否则是一个致命错误。

trait PropertiesTrait {

public $x = 1;

}

class PropertiesExample {

use PropertiesTrait;

}

$example = new PropertiesExample;

$example->x;

需要特别注意的几点

上面这些就是 Trait 基本的使用了,更详细的可以参k官方手册。

这里总结下注意的几 点:

– Trait 会覆盖调用类继承的父类方法

– Trait 无法如 Class 一样使用 new 实例化

– 单个 Trait 可由多个 Trait 组成

– 在单个 Class 中,可以使用多个 Trait

– Trait 支持修饰词(modifiers),例如 final、static、abstract

– 我们能使用 insteadof 以及 as 操作符解决 Trait 之间的冲突

文章来源:智云一二三科技

文章标题:PHP中实现代码复用的Trait方法的一些特性

文章地址:https://www.zhihuclub.com/152151.shtml

关于作者: 智云科技

热门文章

网站地图