我正在开发一个自定义的ORM,这里是一个简单的例子来演示这个问题:PHP:__call调用,而不是__callStatic
class Foo {
public static function __callStatic($name, $args){
if($name == "getBar"){
return "Bar";
}
return NULL;
}
public function __call($name, $args){
if($name == "getFoo"){
$model = "Foo";
$method = "getBar";
return $model::$method();
}
return NULL;
}
}
class Foo2 {
public function __call($name, $args){
if($name == "getFoo"){
$model = "Foo";
$method = "getBar";
return $model::$method();
}
return NULL;
}
}
echo Foo::getBar();//Bar
$foo = new Foo;
var_dump($foo->getFoo()); //Null though I'm expecting Bar
$foo = new Foo2;
var_dump($foo->getFoo()); //Bar
那么,为什么同样的方法中富调用时触发__call
和内部Foo2
触发__callStatic
?
有趣的事情。很显然'__callStatic'只能从其他类中运行,而不能从其他类运行?很高兴知道! – Xatenev
看起来更像是你不能从'__call'调用'__callStatic'。或者,PHP可能会因堆栈超载而失败,或者由于某种原因禁用了此功能。 – tilz0R
@ tilz0R第3种方法从__call调用。 – Xatenev