[英]Why I can access private property in parent php class?
We recently found this strange PHP behaviour. 我们最近发现了这种奇怪的PHP行为。 Accessing a private in the parent class should not work.
在父类中访问私有资源不起作用。 Is this a feature?
这是功能吗? Maybe someone can explain it.
也许有人可以解释。
// PHP classes
class Father {
// private property
private $value = 'test';
}
Class Child extends Father {
// Should fail, se
public function setValue() {
$this->value = 'why does';
}
public function getValue() {
return $this->value;
}
}
$c = new Child();
// should fail!
$c->setValue();
echo $c->getValue() . "|";
// should fail!!!!!!!
$c->value = "it work?";
echo $c->getValue();
// output: why does|it work?
您不是要更改父亲的私有财产,而是要为孩子引入新的财产value
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.