Я новичок в ООП, поэтому, пожалуйста, не будьте резкими.
Моя задача заключается в следующем:
$color = new Color(127,0,0);
$rect = new Rectangle($color, 100, 50);
$rect->render();
Должен принести на страницу следующий код:
"div style="background-color:RGB(127,0,0);width:100px;height:50px"></div>"
Ниже мой код ООП. Цель состояла в том, чтобы использовать абстрактный класс Component
и абстрактный метод render()
. Я пытаюсь выяснить, почему код не работает:
class Color {
protected $red;
protected $green;
protected $blue;
public function __construct($red, $green, $blue) {
$this->red = $red;
$this->green = $green;
$this->blue = $blue;
}
}
abstract class Component {
protected $color;
protected $width;
protected $height;
public function __construct($color) {
$this->color = new Color();
}
abstract function render();
}
class Rectangle extends Component {
public function __construct($color, $width, $height){
parent::__construct();
$this->color = $color;
$this->width = $width;
$this->height = $height;
}
public function render() {
echo "<div style='background-color:RGB(" . $this->color . ");width:" . $this->width . "px;height:" . $this->height . "px'></div>";
}
}
$color = new Color(127,0,0);
$rect = new Rectangle($color, 100, 50);
echo $rect->render();