PHP is handling incorrectly my static call

前端 未结 5 1078
野趣味
野趣味 2020-12-17 05:34

I\'m havinh a problem on PHP 5.3. I need to call a method by using __callStatic, but if I use it in a instancied object, PHP call __call instead.

5条回答
  •  悲&欢浪女
    2020-12-17 06:27

    After thinking about it, it's not really a bug. But it's definitely unintuitive.

    foo();
    

    This is valid, and calls bar(). It does not mean call bar statically. It means to look up the current class name and call function bar if it exists, whether or not it's a static function.

    To clarify a bit more: do you think parent::bar() means to call a static function called bar()? No, it means call whatever function is named bar().

    Consider:

    foo();
    

    The parent::nonExistant() method invokes A::__call(), as does static::nonExistant(). Invoking A::__callStatic() for either call would be equally as valid! There is no way for PHP to know which one you want to be called. However, by design, PHP gives __call the priority when invoked from this sort of context.

    parent::regularMethod() invokes the non static function regularMethod(). (Again, the :: operator does not mean "call this static function.") Likewise parent::staticMethod() invokes A::staticMethod() as one might expect.

    To solve your problem:

    You could manually call self::__callStatic('foo') in the inherited class.

    Or within the __call method of class A filter against a known list and call self::__callStatic.

    function __call($f, $a)
    {
      if ($f == 'foo')
        return self::__callStatic($f, $a); 
      else
      {
      }
    }
    

    It's ugly, but at least people who extend the class won't need to do anything special.

提交回复
热议问题