我正在阅读有关 LSB 功能的 php 手册,我了解它在静态上下文中的工作原理,但在非静态上下文中我不太了解它。手册中的示例是这样的:
<?php
class A {
private function foo() {
echo "success!\n";
}
public function test() {
$this->foo();
static::foo();
}
}
class B extends A {
/* foo() will be copied to B, hence its scope will still be A and
* the call be successful */
}
class C extends A {
private function foo() {
/* original method is replaced; the scope of the new one is C */
}
}
$b = new B();
$b->test();
$c = new C();
$c->test(); //fails
?>
输出是这样的:
success!
success!
success!
Fatal error: Call to private method C::foo() from context 'A' in /tmp/test.php on line 9
我不明白B类,A中的私有方法如何继承给B?谁能带我了解这里发生了什么?非常感谢!