-1
私はoopを学んでいます。 私はoopをカバーし、oopの背後にあるアイデアを理解しようとします。 私はコードを持っており、私は理解のために静的保護を使用したいと思っています。 私は属性を宣言します:protected static $formula
。 protected static $formula
にself :: $formula = $this->width * $this->height;
で電話します。 デバッガでコードを実行すると、私は$formula = null
を得ました。 `$ formula 'は10000でなければなりません。 私はなぜそれが分かりませんか? ありがとうございました。保護されたスタティックでoopで使いたい
<?php
Class Rectangle {
//Declare the attributes:
public $width = 0;
public $height = 0;
protected static $formula = 0;
//Method to set the dimensions.
Function set_size($w = 0, $h = 0) {
$this->width = $w;
$this->height = $h;
self :: $formula = $this->width * $this->height;
}
//Method to calculate and return the area.
function get_area() {
$this->set_size(100,100);
return ($formula);
}
}
$rect = new Rectangle();
echo $rect->get_area();
>