Trait 是什么东西

PHP官方手册里面写的内容是php

自 PHP 5.4.0 起,PHP 实现了一种代码复用的方法,称为 trait。oop

Trait 是为相似 PHP 的单继承语言而准备的一种代码复用机制。Trait 为了减小单继承语言的限制,使开发人员可以自由地在不一样层次结构内独立的类中复用 method。Trait 和 Class 组合的语义定义了一种减小复杂性的方式,避免传统多继承和 Mixin 类相关典型问题。this

Trait 和 Class 类似,但仅仅旨在用细粒度和一致的方式来组合功能。 没法经过 trait 自身来实例化。它为传统继承增长了水平特性的组合;也就是说,应用的几个 Class 之间不须要继承。spa

<?php
trait Trait_a {
    function getReturnType() { /*1*/ }
    function getReturnDescription() { /*2*/ }
}

trait Trait_b {
    function getReturnType() { /*1*/ }
    function getReturnDescription() { /*2*/ }
}

class class1 extends parent_class1 {
    use Trait_a;
    /* ... */
}

class class2 extends parent_class2 {
    use Trait_a,Trait_b;
    /* ... */
}

 

使用use语句利用这个特性。经过逗号分隔,在 use 声明列出多个 trait,能够都插入到一个类中。.net

从基类继承的成员会被 trait 插入的成员所覆盖。优先顺序是来自当前类的成员覆盖了 trait 的方法,而 trait 则覆盖了被继承的方法。code

也就是 当前类 > trait > 父类blog

若是两个 trait 都插入了一个同名的方法,若是没有明确解决冲突将会产生一个致命错误。继承

为了解决多个 trait 在同一个类中的命名冲突,须要使用 insteadof 操做符来明确指定使用冲突方法中的哪个。ip

以上方式仅容许排除掉其它方法,as 操做符能够 为某个方法引入别名。 注意,as 操做符不会对方法进行重命名,也不会影响其方法。开发

冲突的解决

在本例中 Talker 使用了 trait A 和 B。因为 A 和 B 有冲突的方法,其定义了使用 trait B 中的 smallTalk 以及 trait A 中的 bigTalk。

Aliased_Talker 使用了 as 操做符来定义了 talk 来做为 B 的 bigTalk 的别名。

<?php
trait A {
    public function smallTalk() {
        echo 'a';
    }
    public function bigTalk() {
        echo 'A';
    }
}

trait B {
    public function smallTalk() {
        echo 'b';
    }
    public function bigTalk() {
        echo 'B';
    }
}

class Talker {
    use A, B {
        B::smallTalk insteadof A;
        A::bigTalk insteadof B;
    }
}

class Aliased_Talker {
    use A, B {
        B::smallTalk insteadof A;
        A::bigTalk insteadof B;
        B::bigTalk as talk;
    }
}
?>

 

Note:

在 PHP 7.0 以前,在类里定义和 trait 同名的属性,哪怕是彻底兼容的也会抛出 E_STRICT(彻底兼容的意思:具备相同的访问可见性、初始默认值)。

修改方法的访问控制

使用 as 语法还能够用来调整方法的访问控制。

修改方法的访问控制

<?php
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

正如 class 可以使用 trait 同样,其它 trait 也可以使用 trait。在 trait 定义时经过使用一个或多个 trait,可以组合其它 trait 中的部分或所有成员。

从 trait 来组成 trait

<?php
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 的抽象成员

为了对使用的类施增强制要求,trait 支持抽象方法的使用。

 表示经过抽象方法来进行强制要求

<?php
trait Hello {
    public function sayHelloWorld() {
        echo 'Hello'.$this->getWorld();
    }
    abstract public function getWorld();
}

class MyHelloWorld {
    private $world;
    use Hello;
    public function getWorld() {
        return $this->world;
    }
    public function setWorld($val) {
        $this->world = $val;
    }
}
?>

 

Trait 的静态成员

Traits 能够被静态成员静态方法定义。

Example 静态变量

<?php
trait Counter {
    public function inc() {
        static $c = 0;
        $c = $c + 1;
        echo "$c\n";
    }
}

class C1 {
    use Counter;
}

class C2 {
    use Counter;
}

$o = new C1(); $o->inc(); // echo 1
$p = new C2(); $p->inc(); // echo 1
?>

 

Example  静态方法

<?php
trait StaticExample {
    public static function doSomething() {
        return 'Doing something';
    }
}

class Example {
    use StaticExample;
}

Example::doSomething();
?>

 

Trait 一样能够定义属性。

Example  定义属性

<?php
trait PropertiesTrait {
    public $x = 1;
}

class PropertiesExample {
    use PropertiesTrait;
}

$example = new PropertiesExample;
$example->x;
?>

 

Trait 定义了一个属性后,类就不能定义一样名称的属性,不然会产生 fatal error。 有种状况例外:属性是兼容的(一样的访问可见度、初始默认值)。 在 PHP 7.0 以前,属性是兼容的,则会有 E_STRICT 的提醒。

Example  解决冲突

<?php
trait PropertiesTrait {
    public $same = true;
    public $different = false;
}

class PropertiesExample {
    use PropertiesTrait;
    public $same = true; // PHP 7.0.0 后没问题,以前版本是 E_STRICT 提醒
    public $different = true; // 致命错误
}
?>
相关文章
相关标签/搜索