5

I found this weird behaviour with PHP classes (v5.3.8).

You have:

class foo {
  function __call($func, $args) {
    if ($func == 'bar')
      echo "non-static __call";
  }

  static function __callStatic($func, $args) {
    if ($func == 'bar')
      echo "__callStatic";
  }

  function callMe() {
    self::bar();
  }
}

Then you do:

foo::bar() // outputs '__callStatic' as expected.
$f = new foo;
$f->callMe(); // outputs 'non-static __call', as I did not expect.

You see, a non-existent static method called from a non-static function triggers __call() instead of __callStatic(). I was wondering if this is supposed to work like this or is this some kind of bug?

[EDIT]

I forgot to try static::bar(); on callMe() but no, it didn't work either.

I (think I) understand inhan's comment but still... if I'm calling the class itself, not the instance or object, immediate logic for me says it should trigger __callStatic(). Oh well.

Thank you for your answers/comments.

5
  • 1
    PHP has a huge pile of these weird behaviours... :/ The manual says "__callStatic is triggered when invoking inaccessible methods in a static context", which suggests that this might be the expected behaviour. but it certainly doesn't make much sense. it's totally counterintuitive Feb 27, 2012 at 17:33
  • 2
    callMe is a non-static function (in the object scope) so even though you're calling a non-existent method, you're still dealing with the object instance itself, so naturally __call is executed.
    – inhan
    Feb 27, 2012 at 17:39
  • It is weird, though. If you change self::bar(); to foo::bar(), it will still be invoked non-statically, even though that is clearly a static call.
    – webbiedave
    Feb 27, 2012 at 18:08
  • 1
    @webbiedave, foo::bar() is not clearly a static call. It is perfectly valid to call a non-static method like that from within a class to specify exactly which method to call in the case of inheritance.
    – Matthew
    Feb 27, 2012 at 22:48
  • @Matthew: Of course you're correct. My cat got at the keyboard again.
    – webbiedave
    Feb 27, 2012 at 23:05

2 Answers 2

4

You might be confused by what these things mean from within the context of a class method:

class B extends A {
  public function test() {
    A::foo();
    self::foo();
    static::foo();
  }
}

None of those mean "call the static method named foo." It simply means "call the method named foo" at the place in the inheritance tree as specified by what is left of the colons.

Normally, without magic, you only have one function named foo, so the meaning is straightforward. However, when you overload with both magic methods, the call is ambiguous. PHP defaults to using __call() before __callStatic().

1

Static methods, variables belongs to classes not to objects so i think this is supposed to work like this.

4
  • huh? could you explain a bit further? I don't see the connection Feb 27, 2012 at 17:41
  • Some explanation: en.wikipedia.org/wiki/…
    – Peter Kiss
    Feb 27, 2012 at 17:42
  • you don't need to explain that to me. So, again, why do you think it supposed to work like this? Feb 27, 2012 at 17:53
  • My bad. inhand had explained the main concept of this behavior.
    – Peter Kiss
    Feb 27, 2012 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.