因此,我一直在阅读有关Late Static Bindings的官方 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
有人可以解释为什么私有方法 foo() 被复制到 B 吗?据我所知,只有公共和受保护的属性会被复制到子类。我错过了什么?