class Foo {
public static function foobar() {
self::whereami();
}
protected static function whereami() {
echo 'foo';
}
}
class Bar extends Foo {
protected static function whereami() {
echo 'bar';
}
}
Foo::foobar();
Bar::foobar();
Run Code Online (Sandbox Code Playgroud)
预期结果foobar
实际结果foofoo
更糟糕的是,服务器仅限php 5.2
您需要做的只是一个单词的更改!
问题出在调用whereami()的方式上,而不是self ::,您应该使用static ::。因此,Foo类应如下所示:
class Foo {
public static function foobar() {
static::whereami();
}
protected static function whereami() {
echo 'foo';
}
}
Run Code Online (Sandbox Code Playgroud)
换句话说,“静态”实际上使对whereami()的调用成为动态的:)-它取决于调用所处的类。
归档时间: |
|
查看次数: |
7432 次 |
最近记录: |