call_user_func

Call the callback given by the first parameter

Syntax

call_user_func ( callable $callback [, mixed $... ] ) : mixed

Parameters

callback

The callable to be called.

...

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";
$param = "Hello";

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

echo $return;

?>
myfunction Hello

3

<?

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

$callback = $function;
$param = "Hello";

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

echo $return;

?>
{closure} Hello

4

<?

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

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

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

echo $return;

?>
myclass::myfunction Hello

5

<?

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

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

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

echo $return;

?>
myclass::myfunction Hello

6

<?

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

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

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

echo $return;

?>
myclass::myfunction Hello

7

<?

namespace mynamespace;

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

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

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

echo $return;

?>
mynamespace Hello

8

<?

namespace mynamespace;

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

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

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

echo $return;

?>
mynamespace Hello

PHP

Home Menu