Faking Late Static Binding before php 5.3

前端 未结 6 1641
天命终不由人
天命终不由人 2021-01-04 13:04

I need an inherited static function \"call\" to call another static function \"inner\" that has been overridden. I could do this with late static binding, but my host does

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-04 13:32

    Unfortunately there is no nice way to do it (otherwise PHPers wouldn't cheer so much for that feature).

    You have to pass class name. PHP < 5.3 also doesn't have nice syntax for static calls with dynamic class name which makes whole thing even uglier:

    static function call($class)
    {
        return call_user_func(array($class,"inner"));
    }
    …
    ClassA::call("ClassA");
    ClassB::call("ClassB");
    

    If you can change the code (and not use static), then singletons make it more bearable. You can make helper function to ease the syntax:

    X("ClassA")->call();
    X("ClassB")->call();
    

    The X function should look up, create and return instance of a class.

提交回复
热议问题