2012-05-03 7 views
-1

私はoopを学んでいます。 私はoopをカバーし、oopの背後にあるアイデアを理解しようとします。 私はコードを持っており、私は理解のために静的保護を使用したいと思っています。 私は属性を宣言します:protected static $formulaprotected static $formulaself :: $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(); 

>

答えて

4

あなたのコードはget_areaの小さなバグがいる:?

return ($formula); 

は次のようになります。

return self::$formula; 
ここに私のコードです
関連する問題