5
如果呼叫在类未定义的方法制作,魔术方法__call可以拦截电话,这样我就可以处理这种情况,我认为合适的: http://www.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.methods是否有像php中的全局范围魔术方法__call()函数?
有没有在PHP,从而提供任何机制我可以用全球范围内的功能做同样的事情。 点最好用的代码所示:
<?php
function return_some_array(){
$a = array();
//Do stuff to array
return array();
}
// Now i call the function like so:
$give_me_array = return_some_array();
// But sometimes I want the array to not contain zeroes, nulls etc.
// so I call:
$give_me_array_filtered = return_some_array_filtered();
// But i haven't defined return_some_array_filtered() anywhere.
// Instead I would like to do something like so:
function __magic_call($function_name_passed_automatically){
preg_match('/(.*)_filtered$/', $function_name_passed_automatically, $matches);
$function_name_that_i_defined_earlier_called_return_some_array = $matches[1];
if($matches){
$result = call_user_func($function_name_that_i_defined_earlier_called_return_some_array);
$filtered = array_filter($result);
return $filtered;
}
}
//So now, I could call return_some_other_array_filtered() and it would work provided I had defined return_some_other_array().
//Or even Donkey_filtered() would work, provided I had defined Donkey() somewhere.
?>
这是在所有可能的?
谢谢!我想我会试试看。 –
@ V-A这当然是相当浪费和不必要的琐碎事情。你应该像平常一样定义和调用你的函数。 – deceze
另一个说明:你认为这可以作为一个扩展来实现吗?我会想象它会相当简单? –