How to call a static child function from a parent static function?

How to call a child function from a parent static function?

In php5.3, there is a built-in method called get_called_class() to call the child method from the parent class. But my server works with php 5.1 .

Can this be done?

I want to call it from a static function. So I can not use "$ this"

Therefore, I have to use the self keyword.

Below my parent class is "Test123", from the static function of the parent class, "myfunc" is trying to call a function of the child class, such as "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(); 
+6
source share
3 answers

Edit: What you are trying to achieve is not possible with PHP 5.1. In PHP 5.1, there are no late static bindings for PHP Manual , you need to explicitly name the child class to call the child function: Test123456::test() , self will be Test123 in the static function of the Test123 class (always), and the static not available for calling the static function in PHP 5.1.

Related: the new self versus the new static ; PHP 5.2 Equivalent to late static binding (new static)?


If you are referring to a static parent function, you need to explicitly specify the parent (or child) function call in php 5.1:

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

In PHP 5.3, you can do this instead of the static keyword PHP Manual to resolve the name of the called class:

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

If they are non-static, just use the $this PHP Guide :

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

Or, if it has the same name, use the parent PHP Guide :

 parent::parentFunc(); 

(this is not exactly what you requested, just placing it here for completeness).

Get_called_class () was introduced for very specific cases, such as the late static bindings PHP manual .

See Object Inheritance PHP Manual

+11
source

I suspect you are a little confused about abuot parent / child, class / object and function / method.

Ionuลฃ G. Stan provided an explanation of how to call a method that is not declared in the parent class (which, he says, must be abstract or implement the __call () method).

However, if you mean how to call a method that was overridden in the child class from the parent, then this is impossible - and should not be. Consider:

 Class shape { ... } Class circle extends shape { function area() { } } Class square extends shape { function area() { } } 

If you intend to call the area method on an instance of "shape" (which does not have an area method), which child should use it? Both child methods will depend on properties that are not regular / not implemented by the form class.

0
source

try the following:

 <?php class A { public static function newInstance() { $rv = new static(); return $rv; } public function __construct() { echo " A::__construct\n"; } } class B extends A { public function __construct() { echo " B::__construct\n"; } } class C extends B { public function __construct() { echo " C::__construct\n"; } } ?> 
0
source

Source: https://habr.com/ru/post/892673/


All Articles