__call, __callStatic and calling scope in PHP
I recently read about calling scope and scope resolution operator (::) in
PHP. There are two variations: instance calling and statical calling.
Consider the folowing listeng:
<?php
class A {
public function __call($method, $parameters) {
echo "I'm the __call() magic method".PHP_EOL;
}
public static function __callStatic($method, $parameters) {
echo "I'm the __callStatic() magic method".PHP_EOL;
}
}
class B extends A {
public function bar() {
A::foo();
}
}
class C {
public function bar() {
A::foo();
}
}
A::foo();
(new A)->foo();
B::bar();
(new B)->bar();
C::bar();
(new C)->bar();
The result of execution (PHP 5.4.9-4ubuntu2.2) is:
I'm the __callStatic() magic method
I'm the __call() magic method
I'm the __callStatic() magic method
I'm the __call() magic method
I'm the __callStatic() magic method
I'm the __callStatic() magic method
I don't understand why for (new C)->bar(); execute __callStatic() of A?
Instance calling should made in the context of bar() method, isn't it? Is
it feature of PHP?
No comments:
Post a Comment