PHP is handling incorrectly my static call

前端 未结 5 1080
野趣味
野趣味 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:16

    First, make sure you're using PHP 5.3.0 or newer, as that's when __callStatic was implemented. It all seems to work as expected, see this example:

    foo('abc');
    A::bar('123');
    
    class B extends A {
        function invokeStatic($args) {
            echo 'B::invokeStatic', PHP_EOL;
            self::someStatic($args);
        }
    }
    
    $b = new B;
    $b->invokeStatic('456');
    ?>
    

    The output should be (or at least, it is for me):

    A::__call, array (
      0 => 'foo',
      1 => 
      array (
        0 => 'abc',
      ),
    )
    A::__callStatic, array (
      0 => 'bar',
      1 => 
      array (
        0 => '123',
      ),
    )
    B::invokeStatic
    A::__callStatic, array (
      0 => 'someStatic',
      1 => 
      array (
        0 => '456',
      ),
    )
    

    When I run your example above, I just get "Fine!" as the only output, which is exactly what I would have expected.

    Exactly what version of PHP are you running? You can check with this:

    $ php -r 'echo phpversion(), PHP_EOL;'
    5.3.3-7+squeeze1
    

提交回复
热议问题