call_user_func_array

Call a callback with an array of parameters

Syntax

call_user_func_array ( callable $callback , array $param_arr ) : mixed

Parameters

callback

The callable to be called.

param_arr

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

Return

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

Examples

1

<?

function myfunction($arg1, $arg2) {
    return __FUNCTION__ . " $arg1 $arg2";
}

$callback = "myfunction";
$param_arr = array("Hello", "World!");

$return = call_user_func_array($callback, $param_arr);

echo $return;

?>
myfunction Hello World!

2

<?

$function = function($arg1, $arg2) {
    return __FUNCTION__ . " $arg1 $arg2";
};

$callback = $function;
$param_arr = array("Hello", "World!");

$return = call_user_func_array($callback, $param_arr);

echo $return;

?>
{closure} Hello World!

3

<?

class myclass {
    static function myfunction($arg1, $arg2) {
        return __METHOD__ . " $arg1 $arg2";
    }
}

$callback = "myclass::myfunction";
$param_arr = array("Hello", "World!");

$return = call_user_func_array($callback, $param_arr);

echo $return;

?>
myclass::myfunction Hello World!

4

<?

class myclass {
    static function myfunction($arg1, $arg2) {
        return __METHOD__ . " $arg1 $arg2";
    }
}

$callback = array("myclass", "myfunction");
$param_arr = array("Hello", "World!");

$return = call_user_func_array($callback, $param_arr);

echo $return;

?>
myclass::myfunction Hello World!

5

<?

class myclass {
    static function myfunction($arg1, $arg2) {
        return __METHOD__ . " $arg1 $arg2";
    }
}

$callback = array(new myclass(), "myfunction");
$param_arr = array("Hello", "World!");

$return = call_user_func_array($callback, $param_arr);

echo $return;

?>
myclass::myfunction Hello World!

6

<?

namespace mynamespace;

class myclass {
    static function myfunction($arg1, $arg2) {
        return __NAMESPACE__ . " $arg1 $arg2";
    }
}

$callback = __NAMESPACE__ . "\myclass::myfunction";
$param_arr = array("Hello", "World!");

$return = call_user_func_array($callback, $param_arr);

echo $return;

?>
mynamespace Hello World!

7

<?

namespace mynamespace;

class myclass {
    static function myfunction($arg1, $arg2) {
        return __NAMESPACE__ . " $arg1 $arg2";
    }
}

$callback = array(__NAMESPACE__ . "\myclass", "myfunction");
$param_arr = array("Hello", "World!");

$return = call_user_func_array($callback, $param_arr);

echo $return;

?>
mynamespace Hello World!

8

<?

function myfunction(&$arg1, &$arg2) {
    $arg1 = "Hello";
    $arg2 = "World!";
    echo __FUNCTION__ . " $arg1 $arg2";
}

$arg3 = "3";
$arg4 = "4";

$callback = "myfunction";
$param_arr = array(&$arg3, &$arg4);

call_user_func_array($callback, $param_arr);

echo "\nglobal $arg3 $arg4";

?>
myfunction Hello World!
global Hello World!

PHP

Home Menu