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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |