我正在学习oop。
我试图覆盖oop并理解oop背后的想法。
我有代码,我想使用保护静态来理解。
我声明属性:protected static $formula
。
我通过protected static $formula
致电self :: $formula = $this->width * $this->height;
。
当我在debuger中运行代码时,我得到了$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();
&GT;
答案 0 :(得分:4)
您的代码在get_area
中有一个小错误:
return ($formula);
应该是:
return self::$formula;