2
function HelloFunc($Arg1 , $Arg2 , $Arg3 ){
    echo "Arg1 : " . $Arg1 . "\n";
    echo "Arg2 : " . $Arg2 . "\n";
    echo "Arg3 : " . $Arg3 . "\n";
    return "done";
}

Comparing Outputs

echo call_user_func("HelloFunc" , "1" , "2" , "3");


Arg1 : 1
Arg2 : 2
Arg3 : 3
done

echo call_user_func_array("HelloFunc" , "1" , "2" , "3"); 

Warning: call_user_func_array() expects exactly 2 parameters

My question is, the best cases to use call_user_func & call_user_func_array

I know call_user_method & call_user_method_array are depreciated and does not work in PHP7. And call_user_func & call_user_func_array do not work in older PHP.

Is that a good idea to relate following to make it compatible with most php version

call_user_method = call_user_func 
call_user_method_array = call_user_func_array

Like :

function_exists('call_user_method ') ? call_user_method() : call_user_func ();

function_exists('call_user_method_array ') ? call_user_method_array () : call_user_func_array();
4
  • for call_user_func_array, As the name states that it should an array. So use call_user_func_array('funntion', $arr), For your question echo call_user_func_array("HelloFunc" , array("1" , "2" , "3")); Commented May 30, 2016 at 6:06
  • @FrayneKonok That's fine. But how does that make difference as same array could be passed to call_user_func along with call_user_func_array. Is that just that call_user_func_array accepts two arguments, & call_user_func accepts many Commented May 30, 2016 at 6:09
  • Yes, you are right this time. Commented May 30, 2016 at 6:10
  • @FrayneKonok which is right to use in terms of efficiency. Why is there extra function call_user_func_array when things could just happen with call_user_func . I think there should be some difference in usage Commented May 30, 2016 at 6:13

1 Answer 1

3

You can use both call_user_func_array and call_user_func, but depending on your data.

For call_user_func_array, As the name states that it should an array. So use call_user_func_array('funntion', $arr).

For your question echo call_user_func_array("HelloFunc" , array("1" , "2" , "3"));.

How is your HelloFunc function for this:

function HelloFunc($arg1, $arg2, $arg3){

}

More details: function.call-user-func-array.php, function.call-user-func.php

Sign up to request clarification or add additional context in comments.

13 Comments

Got this -> Warning: Missing argument 2 for HelloFunc()
You need to make your function and also parameter like as you pass from this.
$arr = array("1" , "2" , "3"); call_user_func_array('funntion', $arr) & call_user_func('funntion', $arr); Do they make any difference ?
call_user_func("function_name", 'any_parameter_you_can_pass'); but call_user_function_array('function_name', 'only_array')
mixed call_user_func ( callable $callback [, mixed $parameter [, mixed $... ]] ), see the mixed in front of parameter.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.