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.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With