![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
PHP5类关于private我做了一个这样的小程序,有点疑问:
代码如下:
[PHP]
<?php
class animal {
private $name;
private $id;
private $price;
public function __construct($name,$price)
{
$this->price = floatval($price);
$this->name = $name;
$this->id = uniqid();
}
public function equals($c2)
{
return ($c2->name == $this->name && $c2->price == $this->price);
}
public function printP()
{
print($this->name."#".$this->price."
");
}
}
class dog extends animal {
private $color;
public function __construct($name,$price,$color="black")
{
parent::__construct($name,$price);
$this->color = $color;
}
public function equal($a1)
{
return ($this->name == $a1->name && $this->price == $a1->price);
}
public function printP()
{
print($this->name."#".$this->price."#".$this->color);
}
}
$a1 = new animal("dog","3.15");
$d1 = new dog("dog","3.15");
$a1->printP();
$d1->printP();
?>
[/PHP]
我想应该得出的结果是
dog#3.15
dog#3.15#black
但是结果是
dog#3.15
##black
如果将 dog 类中的 printP 中换成
parent::printP();
结果显示正常
但是print_r($d1)又有相关的属性值存在
测试环境是 PHP Version 5.0.5 + apache2
请问是什么原因呢

