【发布时间】:2012-04-21 01:43:26
【问题描述】:
我正在阅读有关 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?谁能带我了解这里发生了什么?非常感谢!
【问题讨论】:
-
一般注意事项:您可能不应该使用
::来调用非静态属性或方法。 -
@mc10 -
static::是您在 PHP 5.3 中调用 late static binding 的方式。但我同意你应该为静态成员保留它。