-1

我正在学习oop。我试图涵盖 oop 并理解 oop 背后的想法。我有代码,我想使用保护静态来理解。我声明属性:protected static $formula. 我打电话给 protected static $formulaby self :: $formula = $this->width * $this->height;。当我在调试器中运行代码时,我得到了$formula = null. `$formula' 应该是 = 10000。我不知道为什么?谢谢你的帮助。这是我的代码:

<?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

1 回答 1

4

您的代码有一个小错误get_area

return ($formula);

应该:

return self::$formula;
于 2012-05-03T07:07:29.770 回答