I'm using PHP 5.4.25. Take the following code:
<?php
function works($params=[])
{
var_dump(is_array($params));
var_dump($params);
$params = (array) $params;
var_dump(is_array($params));
var_dump($params);
$params['first_name'] = "Bob";
}
function fails($params=[])
{
var_dump(is_array($params));
var_dump($params);
$params['first_name'] = "Bob";
}
call_user_func_array('works', ['first_name'=>'John', 'last_name'=>'Doe']);
call_user_func_array('fails', ['first_name'=>'John', 'last_name'=>'Doe']);
Results:
bool(false)
string(4) "John"
bool(true)
array(1) {
[0] =>
string(4) "John"
}
bool(false)
string(4) "John"
PHP Warning: Illegal string offset 'name' in /vhosts/site/test.php on line 14
PHP Stack trace:
PHP 1. {main}() /vhosts/site/test.php:0
PHP 2. call_user_func_array() /vhosts/site/test.php:17
PHP 3. fails() /vhosts/site/test.php:17
It appears that the array passed is not a valid array... It shows only the first value of the array and as a string instead of an array... But you can then cast the string as an array and then it works fine.....
The solution is obviously to just cast it as an array at the beginning of the function, but it seems like you shouldn't have to do this. Is this a PHP bug? I can't seem to find any other reports of this.
fails:call_user_func_array('fails', [['first_name'=>'John', 'last_name'=>'Doe']]);