This extension allows call a callback with an map of parameters function.
% phpize
% ./configure
% make
$ make install
callmap.ini:
extension=callmap.so
callmap.override_call_user_func_array=0 ; PHP_INI_SYSTEM
callmap.override_forward_static_call_array=0 ; PHP_INI_SYSTEM
Override to call_user_func_map the call_user_func_array function.
Override to forward_static_call_map the forward_static_call_array function.
- call_user_func_map — Call a callback with an map of parameters
- forward_static_call_map — Call a static method and pass the arguments as map
mixed call_user_func_map ( callable $callback , array $params )
Calls the callback given by the first parameter with the parameters in params.
-
callback
The callable to be called.
-
params
The parameters to be passed to the callback, as an indexed map.
Returns the return value of the callback, or FALSE on error.
mixed forward_static_call_map ( callable $callback , array $params )
Calls a user defined function or method given by the function parameter.
-
callback
The callable to be called.
-
params
The parameters to be passed to the callback, as an indexed map.
Returns the return value of the callback, or FALSE on error.
function add($a, $b) {
return $a + $b;
}
function sub($c, $d) {
return $c - $d;
}
call_user_func_map('add', ['a' => 3, 'b' => 5, 'c' => 7, 'd' => 11]);
// int(8)
call_user_func_map('sub', ['a' => 3, 'b' => 5, 'c' => 7, 'd' => 11]);
// int(-4)
function test($a, $b, $c, $d) {
//$a -- 'A'
//$b -- 'B'
//$c -- 'C'
//$d -- 'D'
}
call_user_func_map('test', array('d' => 'D', 'a' => 'A', 'c' => 'C', 'b' => 'B'));
function test($a, $b, $c, $d) {
//$a -- 'A'
//$b -- 'D'
//$c -- 'C'
//$d -- 'B'
}
call_user_func_map('test', array('D', 'a' => 'A', 'c' => 'C', 'B'));
function test($a, $b, $c, $d) {
//$a -- 'A'
//$b -- NULL
//$c -- 'C'
//$d -- ?
}
call_user_func_map('test', array('a' => 'A', 'c' => 'C'));
function test($a = '1', $b = '2', $c = '3', $d = '4') {
//$a -- 'A'
//$b -- '2' //version 0.2.0 or newer (0.1.0 is NULL)
//$c -- 'C'
//$d -- '4'
}
call_user_func_map('test', array('a' => 'A', 'c' => 'C'));
callmap.ini (callmap.override_call_user_func_array=1)
var_dump(ini_get('callmap.override_call_user_func_array')); //bool(true)
function test($a, $b) {
return $a . $b;
}
$res1 = call_user_func_array('test', array('b' => '2', 'a' => '1'));
$res2 = call_user_func_map('test', array('b' => '2', 'a' => '1'));
var_dump($res1 === $res2); //bool(true)