私はOOPを新しくしていますので、厳しくしないでください。これはなぜ機能しないのですか? OOPの初心者
"div style="background-color:RGB(127,0,0);width:100px;height:50px"></div>"
以下は私のOOPコードです:
$color = new Color(127,0,0);
$rect = new Rectangle($color, 100, 50);
$rect->render();
ページに次のコードを持参してください:
私の仕事は、これがあることです。目的は、抽象クラス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();
間違っています! )*あなたが*なぜ*になる前に何が働いていないのですか?通常、最初のものは直接2番目のものにつながります。 –
何が失敗していますか?エラーメッセージが表示されていますか?ページが完全に読み込めませんか? –
警告:Component :: __ construct()の引数1が見つからない、26行目/public_html/index.phpで呼び出され、18行目の/public_html/index.phpに定義されています – Sorrybyenglish