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

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

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();
编辑:PHP 5.1无法实现您的目标.在PHP 5.1中没有 late static bindings PHP Manual,你需要显式命名子类来调用函数:Test123456 :: test(),self将是Test123类的静态函数Test123(总是),而static关键字不是可以在PHP 5.1中调用静态函数.

相关:new self vs new static; PHP 5.2 Equivalent to Late Static Binding (new static)?

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

parentClass::func();
Test123456::test();

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

static::func();
static::test();

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

$this->parentFunc();
$this->childFunc();

或者,如果它具有相同的名称,请使用父PHP Manual

parent::parentFunc();

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

Get_called_class()已经针对非常具体的情况引入,例如late static bindings PHP Manual.

Object Inheritance PHP Manual

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...