如何从父静态函数调用静态子函数?

Sah*_*hal 8 php oop static late-static-binding

如何从父静态函数调用子函数?

在php5.3中,有一个内置方法get_called_class(),用于从父类调用子方法.但是我的服务器运行的是php 5.1.

有什么办法可以做到这一点吗?

我想从静态函数中调用它.所以我不能用"$ this"

所以我应该使用"自我"关键字.

下面的示例我的父类是"Test123",来自父类的静态函数"myfunc"我试图像这样调用子类的函数"self :: test();"

abstract class Test123
{

  function __construct()
  {
    // some code here
  }

  public static function myfunc()
  {
    self::test();
  }

  abstract function test();
}

class Test123456 extends Test123
{
  function __construct()
  {
    parent::__construct();
  }

  function test()
  {
    echo "So you managed to call me !!";
  }

}

$fish = new Test123456();
$fish->test();
$fish->myfunc();
Run Code Online (Sandbox Code Playgroud)

hak*_*kre 13

编辑: PHP 5.1无法实现您的目标.在PHP 5.1中没有后期静态绑定PHP手册,您需要显式命名子类以调用子函数:Test123456::test(),selfTest123在类的静态函数中Test123(总是)并且static关键字不可用于调用静态函数在PHP 5.1中.

相关:新自我与新静态 ; PHP 5.2相当于后期静态绑定(新的静态)?


如果您指的是静态父函数,那么您需要在php 5.1中为函数调用显式命名父(或子):

parentClass::func();
Test123456::test();
Run Code Online (Sandbox Code Playgroud)

在PHP 5.3中,您可以使用static关键字PHP Manual来解决被调用类的名称:

static::func();
static::test();
Run Code Online (Sandbox Code Playgroud)

如果这些是非静态的,只需使用$this PHP手册:

$this->parentFunc();
$this->childFunc();
Run Code Online (Sandbox Code Playgroud)

或者,如果它具有相同的名称,请使用parent PHP手册:

parent::parentFunc();
Run Code Online (Sandbox Code Playgroud)

(这不完全是你要求的,只是把它放在这里是为了完整).

Get_called_class()已经被引入用于非常具体的情况,比如后期静态绑定PHP手册.

请参见对象继承PHP手册