I recently read about calling the scope operator and scope operator (: :) in PHP. There are two options: instance call and static call. Consider the following list:
<?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 do not understand why to (new C)->bar();execute __callStatic()from A? An instance call must be made in the context of the bar () method, right? Is this a feature of PHP?
Addition1:
Also, if I don’t use magic methods and explicitly call, everything works as expected:
<?php
class A {
public function foo() {
echo "I'm the foo() method of A class".PHP_EOL;
echo 'Current class of $this is '.get_class($this).PHP_EOL;
echo 'Called class is '.get_called_class().PHP_EOL;
}
}
class B {
public function bar() {
A::foo();
}
}
(new B)->bar();
Result for this:
I'm the foo() method of A class
Current class of $this is B
Called class is B
source
share