标签:php
我试图在SO上引用这个question,但仍然没有得到它.
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();
}
public function foo() {
parent::foo();
}
}
(new B)->bar();
(new B)->foo();
根据我的理解,bar函数静态地调用类A上的foo方法,但是foo方法使用A的实例调用方法,该实例是B的父类.我期望它应该给我:
I'm the __callStatic() magic method
I'm the __call() magic method
但是,显然,我得到:
I'm the __call() magic method
I'm the __call() magic method
解决方法:
…A::foo() is not necessarily a static call. Namely, if foo() is not static and there is a compatible context ($this exists and its class is either the class of the target method or a subclass of it), an instance call will be made.
如果foo()是静态的,它可以按预期工作:
class A {
public function __call($method, $parameters) {
echo "I'm the __call() magic method $method".PHP_EOL;
}
public static function __callStatic($method, $parameters) {
echo "I'm the __callStatic() magic method $method".PHP_EOL;
}
}
class B extends A {
public static function foo() { //
parent::foo();
}
}
(new B)->foo();
I’m the __callStatic() magic method foo
标签:php
来源: https://codeday.me/bug/20190527/1165111.html