I need to call a function with arguments array, but call_user_func_array seems to be very slow for me.
I tried to use ReflectionFunction for that (see code ex. 1), but maybe there is another methods for that? It will be perfectly, if that method supports sorted parameters (see code ex. 2)
code ex. 1
private static function call(Callable $callable, array $args) {
if(class_exists('ReflectionFunction', false)) {
$r = (new ReflectionFunction($callable))->invokeArgs($args);
# ROUTES_CALLBACK_STRATEGY may be "call" or "echo"
if(defined('ROUTES_CALLBACK_STRATEGY') && strtolower(ROUTES_CALLBACK_STRATEGY) == 'call') {
return;
} else {
echo $r;
}
} else {
call_user_func_array($callable, $args);
}
}
code ex. 2
$args = array(
'param1' => 'p1',
'param2' => 'p2',
'custom_name' => 'c_n'
);
$callback = function($param, $other_param, $foo) {
echo $param . " " . $other_param . " " . $foo; // output: p1 p2 c_n
}
$callback2 = function ($custom_name, $param1, $param2) {
echo $custom_name . " " . $param1 . " " . $param2; // output: c_n p1 p2
}
Thanks for help.
call_user_func_arrayis "slow"?call_user_func_arrayis 124% slower than calling function directly (function()), calling withReflectionFunctionis 109% slower.call_user_func_arraywas the way to go. Not sure of any other method (other thanReflectionFunction, which I always thought was even slower).func_get_argswithin your callable