PHP 5.2 Equivalent to Late Static Binding (new static)?

后端 未结 1 970
甜味超标
甜味超标 2021-01-18 15:14

I am trying to make a script that is built for php 5.3 work on a php 5.2 server. The script uses a lot of late static binding like:

return new static($option         


        
1条回答
  •  旧巷少年郎
    2021-01-18 16:09

    I think the only way is to pass by a protected static method that build your singleton and a public static method that defines the class to use. You can "emulate" it by using the get_class function over $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));
        }
    }
    

    0 讨论(0)
提交回复
热议问题