PHP 5.2 equivalent to late static binding (new static)?

I am trying to create a script that is created to run php 5.3 on a php 5.2 server. The script uses a lot of late static bindings, for example:

return new static($options); 

What is equivalent to this in php 5.2? is it somehow new? Or it is impossible to achieve the same effect ...

thanks

EDIT:

Here is a related question. New me versus new static

Juts is trying to wrap my head around this late static binding file ...

+5
source share
1 answer

I think the only way is to go through a protected static method that builds your singleton and a public static method that defines the class to use. You can "emulate" it using the get_class function via $ this

 class ParentClass{ protected static function getInstance2($className){ //some stuffs here return new $className(); } public static function getInstance(){ return self::getInstance2(get_class(self)); } } class ChildClass extends ParentClass{ public static function getInstance(){ return self::getInstance2(get_class(self)); } } 
-1
source

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


All Articles