性状 Trait

2018-08-07  本文已影响0人  WeCat

性状 Trait
性状(Trait)是PHP5.4.0引入的概念,既像接口,也像类,但是两者都不是。
性状有两个作用:1.表明类可以做什么(像接口);2.将代码模块化(像类)。
性状能把模块化的实现方式注入多个无关的类中(类与类之间没有业务功能相似处),而且还能促进代码重构。

性状创建:

trait MyTrait
{
    protected $trait_test_var;
    protected $trait_test_result;

    public function setVar($test_var)
    {
        $this->trait_test_var = $test_var;
        $this->trait_test_result = $this->trait_test_var + 1;
    }

    public function getVarResult()
    {
        return $this->trait_test_result;
    }
}

然后,再创建一个测试类:

// 父类(交通工具)
class Vehicle
{
    protected $driver;
    protected $drive_style;

    public function __construct($driver)
    {
        $this->driver = $driver;
    }

    public function doThing()
    {
        echo 'I am ' . $driver . ' and,i am driving';
    }

}

//子类(小汽车)
class Car extends Vehicle
{
    public function doThing()
    {
        echo 'I am ' . $this->driver . ' and,i am driving car';
    }
}

创建好之后。
在子类car中加入"use MyTrait"

class Car extends Vehicle
{

    use MyTrait;

    public function doThing()
    {
        echo 'I am ' . $this->driver . ' and,i am driving car';
    }
}

然后,使用car:

$car = new Car('jeep');
$car->setVar(1);
var_dump($car->getVarResult()); // 输出 2

如果还有另外一辆车,例如 jeep

class Jeep extends Vehicle
{
    public function doThing()
    {
        echo 'I am ' . $this->driver . ' and i am driving jeep';
    }
}

这辆 jeep也想使用 MyTrait 时,我们也可以在其中加入 "use MyTrait",像这样

class Jeep extends Vehicle
{
    use MyTrait;

    public function doThing()
    {
        echo 'I am ' . $this->driver . ' and i am driving jeep';
    }
}

然后使用:

$car = new Car('jeep');
$car->setVar(1);
var_dump($car->getVarResult()); // 输出 2

$jeep = new Jeep('jeep');
$jeep->setVar(3);
var_dump($jeep->getVarResult());  // 输出 4

但是,这样每一个子类中都使用"use MyTrait "引入,这样做不大好,可以进行优化。
可以在父类Vehicle中使用"use MyTrait"引入,像这样:

class Vehicle
{
    use MyTrait;

    protected $driver;
    protected $drive_style;

    public function __construct($driver)
    {
        $this->driver = $driver;
    }

    public function doThing()
    {
        echo 'I am ' . $driver . ' and,i am driving';
    }

}


class Car extends Vehicle
{

    #use MyTrait;

    public function doThing()
    {
        echo 'I am ' . $this->driver . ' and,i am driving car';
    }
}

class Jeep extends Vehicle
{
    #use MyTrait;

    public function doThing()
    {
        echo 'I am ' . $this->driver . ' and i am driving jeep';
    }
}

这样做,同样可以使用。

完整代码如下:

trait MyTrait
{
    protected $trait_test_var;
    protected $trait_test_result;

    public function setVar($test_var)
    {
        $this->trait_test_var = $test_var;
        $this->trait_test_result = $this->trait_test_var + 1;
    }

    public function getVarResult()
    {
        return $this->trait_test_result;
    }
}


class Vehicle
{
    use MyTrait;

    protected $driver;
    protected $drive_style;

    public function __construct($driver)
    {
        $this->driver = $driver;
    }

    public function doThing()
    {
        echo 'I am ' . $driver . ' and,i am driving';
    }

}


class Car extends Vehicle
{

    #use MyTrait;

    public function doThing()
    {
        echo 'I am ' . $this->driver . ' and,i am driving car';
    }
}

class Jeep extends Vehicle
{
    #use MyTrait;

    public function doThing()
    {
        echo 'I am ' . $this->driver . ' and i am driving jeep';
    }
}




$car = new Car('jeep');
$car->setVar(1);
var_dump($car->getVarResult()); // 输出 2

$jeep = new Jeep('jeep');
$jeep->setVar(3);
var_dump($jeep->getVarResult());
上一篇 下一篇

猜你喜欢

热点阅读