【发布时间】:2014-01-26 14:47:14
【问题描述】:
我是 PHP 新手,这是我的代码
class number_config{
public $length = 200, //declare a properties
$width = 100,
$height = 300;
}
class formula extends number_config{
public function rectangular_prism(){
$volume = $length * $width * $height;
echo 'The rectangular prism volume is: '. $volume .'meter cubic';
// i want this echo produce this,
// The rectangular prism volume is: 6000000 meter cubic
//set the new member_config properties value
$length = 600;
$width = 1000;
$height = 400;
}
public function rectangular_prism2(){
$volume = $length * $width * $height;
echo 'The rectangular prism volume is: '. $volume .'meter cubic';
// i want this echo produce this,
// The rectangular prism volume is: 240000000 meter cubic
//and then set the other new member_config properties value, and so on
$length = 800;
$width = 2000;
$height = 300;
}
}
$rectangular_prism_volume = new formula();
echo $rectangular_prism_volume->rectangular_prism();
echo $rectangular_prism_volume->rectangular_prism2();
首先,我很抱歉这个愚蠢的 php 代码,
我是新手,只是尝试练习:)
该代码不起作用,
它打印出一个错误
通知:未定义变量:xxx.php 中的宽度在第 11 行
通知:未定义变量:xxx.php 中 11 行的长度
注意:未定义变量:高度xxx.php 第 11 行
矩形棱柱体积为:0 米立方
注意:未定义变量:xxx.php 行 23
注意:未定义变量:xxx.php 中的长度,第 23 行
注意:未定义变量:xxx.php 中的高度 23
长方体体积为:0米立方
我想问的是如何获取该父类的属性
在函数上,并分配一个新的属性值,覆盖宽度、长度、高度、属性值
感谢任何帮助我处理此案的人:)
【问题讨论】:
-
使用
$this->width等从类的实例中访问对象属性 -
哇.. 真是太快了.. :D 谢谢你的帮助,它解决了一切,即使我使用的是受保护的属性 :D
标签: php function class properties public