call_user_func

Call the callback given by the first parameter

Syntax

 call_user_func(callable $callback, mixed ...$args): mixed

Parameters

callback

The callable to be called.

args

Zero or more parameters to be passed to the callback.

Note: Note that the parameters for call_user_func() are not passed by reference.

Return

Returns the return value of the callback.

Examples

1

<?

function myfunction()
{
    return __FUNCTION__ . " Hello";
}

$callback = "myfunction";

$return = call_user_func($callback);

echo $return;

?>
myfunction Hello

2

<?

function myfunction($arg)
{
    return __FUNCTION__ . " $arg";
}

$callback = "myfunction";
$args = "Hello";

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

echo $return;

?>
myfunction Hello

3

<?

$function = function($arg)
{
    return __FUNCTION__ . " $arg";
};

$callback = $function;
$args = "Hello";

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

echo $return;

?>
{closure} Hello

4

<?

class myclass
{
    static function myfunction($arg)
    {
        return __METHOD__ . " $arg";
    }
}

$callback = "myclass::myfunction";
$args = "Hello";

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

echo $return;

?>
myclass::myfunction Hello

5

<?

class myclass
{
    static function myfunction($arg)
    {
        return __METHOD__ . " $arg";
    }
}

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

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

echo $return;

?>
myclass::myfunction Hello

6

<?

class myclass
{
    static function myfunction($arg)
    {
        return __METHOD__ . " $arg";
    }
}

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

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

echo $return;

?>
myclass::myfunction Hello

7

<?

namespace mynamespace;

class myclass
{
    static function myfunction($arg)
    {
        return __NAMESPACE__ . " $arg";
    }
}

$callback = __NAMESPACE__ . "\myclass::myfunction";
$args = "Hello";

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

echo $return;

?>
mynamespace Hello

8

<?

namespace mynamespace;

class myclass
{
    static function myfunction($arg)
    {
        return __NAMESPACE__ . " $arg";
    }
}

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

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

echo $return;

?>
mynamespace Hello

call_user_func_array

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