Trait 关键字实现多继承(使用use 引用)

php只支持单继承,使用trait 可以实现多继承的功能,在类中使用use 关键字引用trait
<?php
trait Bt
{
    public function atest()
    {
        echo "hello";
    }

    public function btest()
    {
        echo "world";
    }

    public function ab()
    {
        $this->atest();
        $this->btest();
    }
}

class Test
{
    use Bt;
}

$test = new Test();
$test->ab();

?>

 


posted @ 2017-11-24 21:48  xc_flying  阅读(473)  评论(0编辑  收藏  举报