call_user_func_array

Call a callback with an array of parameters

Syntax

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

Parameters

callback

The callable to be called.

args

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";
$args = array("Hello", "World!");

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

echo $return;

?>
myfunction Hello World!

2

<?

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

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

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

echo $return;

?>
{closure} Hello World!

3

<?

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

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

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

echo $return;

?>
myclass::myfunction Hello World!

4

<?

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

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

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

echo $return;

?>
myclass::myfunction Hello World!

5

<?

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

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

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

echo $return;

?>
myclass::myfunction Hello World!

6

<?

namespace mynamespace;

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

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

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

echo $return;

?>
mynamespace Hello World!

7

<?

namespace mynamespace;

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

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

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

echo $return;

?>
mynamespace Hello World!

8

<?

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

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

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

call_user_func_array($callback, $args);

echo "\nglobal $arg3 $arg4";

?>
myfunction Hello World!
global Hello World!

call_user_func

forward_static_call

forward_static_call_array

func_get_arg

func_get_args

func_num_args

function_exists

get_defined_functions

register_shutdown_function

register_tick_function

unregister_tick_function

PHP

Home Menu