可能重复:
引用子类的静态成员
请查看以下代码以了解我的问题.
<?php
Interface IDoesSomething
{
public static function returnSomething();
}
abstract class MiddleManClass implements IDoesSomething
{
public static function doSomething()
{
return 1337 * self::returnSomething();
}
}
class SomeClass extends MiddleManClass
{
public static function returnSomething()
{
return 999;
}
}
// and now, the vicious call
$foo = SomeClass::doSomething();
/**
* results in a
* PHP Fatal error: Cannot call abstract method IDoesSomething::returnSomething()
*/
?>
Run Code Online (Sandbox Code Playgroud)
有没有办法强制抽象,returnSomething()同时保持从抽象"中间人"类中定义的函数调用函数的可能性?看起来像是我的PHP瓶颈.
如果您的php版本> = 5.3然后更改
public static function doSomething()
{
return 1337 * self::returnSomething();
}
Run Code Online (Sandbox Code Playgroud)
至
public static function doSomething()
{
return 1337 * static::returnSomething();
}
Run Code Online (Sandbox Code Playgroud)