2012-05-03 2 views
-1

나는 oop을 배우고 있습니다. 나는 덮개를 덮고 그 뒤에있는 생각을 이해하려고 노력합니다. 코드가있어서 이해를 위해 static을 사용하고 싶습니다. 속성 선언 : protected static $formula. protected 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; 
여기 내 코드입니다