我正在尝试扩展 PHP 类而不重写整个内容。这是一个例子:
<?
$a = new foo();
print $a->xxx();
$b = new bar();
print $b->xxx();
class foo {
const something = 10;
public function xxx() {
$this->setSomething();
return $this->something;
}
private function setSomething() {
$this->something = self::something;
}
}
class bar extends foo {
public function xxx() {
$this->setSomething();
$this->something++;
return $this->something;
}
}
?>
但是,当我运行脚本时,出现以下错误:
Fatal error: Call to private method foo::setSomething() from context 'bar' in test.php on line 23
看起来 bar 没有继承私有函数 setSomething()。在不修改 foo 类的情况下如何解决这个问题?