我有两个类:
class JController{
public static function getInstance()
{
//some source, not important...
self::createFile();//
}
public static function createFile()
{
// this is base class method
}
}
class CustomController extends JController{
public static function createFile()
{
// this is overriden class method
}
}
我试图在派生类上调用静态方法,该方法调用parents方法而不是overriden。这是预期行为吗?
这就是我尝试使用它的方式:
$controllerInstance = CustomController::getInstance();
我的问题是:为什么CustomController::getInstance()不调用CustomController::createFile()?
这是预期的行为。在php5.3之前,静态方法只会调用层次结构中第一个定义中的方法。5.3+具有后期静态绑定支持,因此能够直接在子类上使用该方法。要做到这一点,您需要使用static
关键字而不是self
:
public static function getInstance()
{
//some source, not important...
static::createFile();//
}
后期静态绑定:
使用
static::createFile();
而不是
self::createFile();