【问题标题】:I want to use in oop with protected static [closed]我想在 oop 中使用受保护的静态 [关闭]
【发布时间】:2012-05-12 16:31:48
【问题描述】:

我正在学习 oop。 我试图涵盖 oop 并理解 oop 背后的想法。 我有代码,我想使用保护静态来理解。 我声明属性:protected static $formula。 我打电话给protected static $formulaself :: $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();

?>

【问题讨论】:

    标签: php oop static protected


    【解决方案1】:

    您的代码在get_area 中有一个小错误:

    return ($formula);
    

    应该是:

    return self::$formula;
    

    【讨论】:

      猜你喜欢
      • 2016-09-13
      • 2016-02-19
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 2011-05-15
      • 2014-12-19
      • 1970-01-01
      相关资源
      最近更新 更多