我們最近發現了這種奇怪的PHP行爲。在父類中訪問私人應用程序不應該工作。這是一個功能嗎?也許有人可以解釋它。爲什麼我可以在父級php類中訪問私有財產?
// 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?
您可能想使用'protected' – Robik