/php-ext-callmap

Call a callback with an map of parameters function Extension for PHP

Primary LanguagePHPOtherNOASSERTION

Call a callback with an map of parameters function Extension for PHP

Build Status

This extension allows call a callback with an map of parameters function.

Build

% phpize
% ./configure
% make
$ make install

Configration

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

callmap.override_call_user_func_array bool

Override to call_user_func_map the call_user_func_array function.

callmap.override_forward_static_call_array bool

Override to forward_static_call_map the forward_static_call_array function.

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

call_user_func_map — Call a callback with an map of parameters

Description

mixed call_user_func_map ( callable $callback , array $params )

Calls the callback given by the first parameter with the parameters in params.

Parameters

  • callback

    The callable to be called.

  • params

    The parameters to be passed to the callback, as an indexed map.

Return Values

Returns the return value of the callback, or FALSE on error.

forward_static_call_map — Call a static method and pass the arguments as map

Description

mixed forward_static_call_map ( callable $callback , array $params )

Calls a user defined function or method given by the function parameter.

Parameters

  • callback

    The callable to be called.

  • params

    The parameters to be passed to the callback, as an indexed map.

Return Values

Returns the return value of the callback, or FALSE on error.

Examples

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)

Example #1

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'));

Example 1

Example #2

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'));

Example 2

Example #3

function test($a, $b, $c, $d) {
    //$a -- 'A'
    //$b -- NULL
    //$c -- 'C'
    //$d -- ?
}
call_user_func_map('test', array('a' => 'A', 'c' => 'C'));

Example 3

Example #4

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'));

Example 4

Example #5

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)

Related