'Why Difference between call_user_func and call_user_func_array() return types
I was trying to get a clear difference between methods call_user_func() and call_user_func_array() when I stumbled upon a difference in return types that wasn't apparent to me before.Consider the class Test with a method dnd($t):
class Test {
function dnd($t) {
echo "<pre>";
var_dump($t);
echo "</pre>";
}
}
Instantiate:
$obj = new Test();
$params = [33,22,'fff'];
Now calling by call_user_func method
call_user_func([$obj,'dnd'], $params);
This results in:
array(3) {
[0]=>
int(33)
[1]=>
int(22)
[2]=>
string(3) "fff"
}
While call_user_func_array:
call_user_func_array([$obj,'dnd'], $params);
yields:
int(33)
Why this variation and what impact does it have on parameters passed to functions or function calls that rely on parameters?
Solution 1:[1]
In the second parameter call_user_func receives the 'arguments' separately.
So our only argument is the array.
$t = [33,22,'fff']
The second parameter call_user_func_array receives the 'arguments' in the form of an array and sends them to the desired function.
As a result, the first array index is used as the only argument.
$t = int(33) // int(22) and 'fff' are the second and third arguments of the dnd() method (not defined here)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
