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){