×

面向对象编程

PHP面向对象——Trait关键字

feihu1996 feihu1996 发表于2018-08-04 17:49:35 浏览221 评论0

抢沙发发表评论

<?php
/*
* @file: example12.php
* @brief: PHP面向对象——Trait关键字
* @author: feihu1996.cn
* @date: 下午14:42 18-8-4
* @version: 1.0
*/

/*
    PHP是单继承语言,
    而使用Trait,可以实现代码复用
*/

trait Power{
    public $isTurnOn = 0;

    public function startPower(){
        $this->isTurnOn = 1;
        echo "start power ...";
        echo "\r\n";
    }
}

trait Source{
    public function useSource(){
        echo "use source ...";
        echo "\r\n";
    }
}

trait Log{
    use Power, Source;

    public function startLog() {
        echo "start log ...";
        echo "\r\n";
    }   
    public function endLog() {
        echo "end log ...";
        echo "\r\n";
    }   
}

class Publish {
    use Log;
    public function __construct(){
        echo $this->isTurnOn;
        echo "\r\n";
    }
    public function __destruct(){
        echo $this->isTurnOn;
        echo "\r\n";
    }    
}
$publish = new Publish();
$publish->startPower();
$publish->useSource();
$publish->startLog();
$publish->endLog();

群贤毕至

访客