Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 具有嵌套类的装饰器模式_Php_Decorator - Fatal编程技术网

Php 具有嵌套类的装饰器模式

Php 具有嵌套类的装饰器模式,php,decorator,Php,Decorator,我有班级结构 class engine { function show() { return "some html code"; } } class car { private $engine; function __construct() { //some logic $this->engine = new engine(); } function show() { $co

我有班级结构

class engine {
    function show() {
        return "some html code";
    }
}

class car {
    private $engine;
    function __construct() {
        //some logic
        $this->engine = new engine();
    }

    function show() {
        $code = "****";
        $code .= $this->engine->show();
        return $code;
    }
}

每个类都有html、svg、txt格式的输出。我尝试使用decorator模式来制作htmlCar、htmlEngine、svgCar、svgEngine,并在不编辑引擎和汽车类的情况下添加更多输出。但是如何在不更改$this->engine=newengine()的情况下执行此操作;要$this->engine=new htmlEngine()

注射它<代码>函数构造(engineInterface$engine){注入它。
函数构造(engineInterface$engine){