静态调用函数时是否可以使用__call
魔术方法?
答案 0 :(得分:15)
还没有,我知道在管道中有一个建议的(现在可用的)__callStatic
Docs方法。否则__call
和其他__
魔术方法除了对象实例之外的任何其他内容都不可用。
答案 1 :(得分:8)
您必须使用其他魔术方法__callStatic
- 这仅适用于PHP> = 5.3,尚未实际发布。
答案 2 :(得分:0)
如前所述,没有神奇的静态调用者。但你可以像这样编码:
class First {
public static function test1(){
return 1;
}
public static function test2(){
return 2;
}
}
class Second {
public static function test1(){
if(func_num_args()>0){
return func_get_args();
}
return 21;
}
public static function test2(){
return 22;
}
}
class StaticFactory {
public static function factory($class, $method){
if(func_num_args()>2){
$args = func_get_args();
array_shift($args);
array_shift($args);
return call_user_func_array(array($class,$method), $args);
}else{
return call_user_func_array(array($class,$method), array());
}
}
}
print_r(StaticFactory::factory("Second", "test1", 1, false, true));
print_r(StaticFactory::factory("First", "test1"));