Call_user_func_array (callable $callback, array $param _arr)
Number 1: Call a callback function,
Parameter 2: Array parameters are the parameters of the callback function.
Call_user_func (callable $callback, $mixed $parameter, $mixed $ ...)
Parameter 1: callback function for Call
2-n: The number of parameters for the callback function.
The difference between the two is different oh. The second parameter of the former must be an array.
Case one: Call the normal function.
<?Phpfunction Barber ($type) {
Here, the first parameter directly specifies the name of the function.
<?Phpfunction Barber ($type) {
Or compare the difference between the two.
Look out please continue.
Scenario Two: Call a static function in the class.
Here are the Call_user_func_arr
Namespace Foo;class F {public static function ShowName ($name) {return strtoupper ($name);}} Echo Call_user_func_array (__namespace__. ' \f::showname ', Array (' vein ')); Echo Call_user_func_array (Array (__namespace__. ' \f ', ' showname '), Array (' vein '), $f = new F (), Echo Call_user_func_array (Array ($f, ' showname '), Array (' vein '));
Here are the Call_user_func
Namespace Foo;class F {public static function ShowName ($name) {return strtoupper ($name);}} Echo Call_user_func (__namespace__. ' \f::showname ', ' vein '); Echo call_user_func (Array (__namespace__. ' \f ', ' showname '), ' vein '); $f = new F (); Echo call_user_func (Array ($f, ' showname '), ' vein ');
Scenario Three: Dynamic method invocation
Call_user_func
Namespace Foo;class F {public Function showage ($age) {return $age + 100;}} Call_user_func (__namespace__. ' \f::showage ', 23);
Note here that if this is called, the system will error, prompting
Call_user_func () expects parameter 1 to am a valid callback, Non-static method Foo\f::showage () should not being called S
Tatically
Explain: The first parameter of this function must be a valid callback function, non-static method Showage () is not agreed to call.
The required solutions are:
Namespace Foo;class F {public static function ShowName ($name) {return strtoupper ($name);} Public Function Showage ($age) {return $age + 100;}} $f = new F (); echo call_user_func (Array ($f, ' showage '), 23);
For the invocation of this dynamic function, object instantiation must be done in advance,
The instantiated object is then passed into the function as the first argument.
Call_user_func_array:
Namespace Foo;class F {public static function ShowName ($name) {return strtoupper ($name);} Public Function Showage ($age) {return $age + 100;}} $f = new F (); echo Call_user_func_array (Array ($f, ' showage '), Array (23));
This is also possible.
To summarize: The two functions of Call_user_func_array and Call_user_func are basically similar, but there are some differences when passing parameters on a call.
Remember that the second parameter passed by the Call_user_func_array must be an array,
The second parameter passed by the Call_user_func may be an array. If you have multiple parameters, you need to list them.
Call_user_func & Call_user_func_array of PHP functions