Ken*_*ogt 8 php inheritance class
我试图扩展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;
}
}
?>
Run Code Online (Sandbox Code Playgroud)
但是当我运行脚本时,我收到以下错误:
Fatal error: Call to private method foo::setSomething() from context 'bar' in test.php on line 23
Run Code Online (Sandbox Code Playgroud)
似乎bar不继承私有函数setSomething().如何在不修改foo类的情况下解决这个问题?
bar是继承正常的功能,但它不能调用它.这是private方法的全部要点,只有声明类可以调用它们.
| 归档时间: |
|
| 查看次数: |
16882 次 |
| 最近记录: |