0

I have lots of lines this kind of code. call('someCustomFunctionName', array($paramA, &paramB, $paramC));

I have updated my server php version and it wont work anymore.

Here is SIMPLIFIED code for testing purpose. Expected output: $b is true and $c is 2

function call($function, $param_arr = array())
{   
    # Lots of code here.
    return function_exists($function) ? call_user_func_array( $function, (array) $param_arr) : '';
}

function test_a($a, $b, $c)
{
    if($a['a'] == 1)
    {
        $b = true;
        $c++;
    }
}

$a = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
$b = false;
$c = 1;

call('test_a', array($a, &$b, &$c));

if($b)
{
    print '$b is true';
}
else{
    print '$b is false';
}
print '<br>';
print $c;
user3524402
  • 19
  • 1
  • 5
  • 2
    What error do you get in the server logs? Or are you just not getting the expected output? – aynber Jan 09 '17 at 17:22
  • 1
    It's a "pass by reference" issue: you're passing by value, but still expecting those variables to be updated as though you were passing by reference. [This may help](http://stackoverflow.com/questions/1905800/php-call-user-func-array-pass-by-reference-issue) – Mark Baker Jan 09 '17 at 17:23
  • 2
    "it does not work" has never helped anyone anywhere at any time to get help with code. You need to be more specific in the details: what _exact_ behavior and error do you get? – arkascha Jan 09 '17 at 17:24
  • 1
    before server php upgrade everything worked. No errors. Expected $b value should be true i get false. Expected $c value should be 2 i get 1 – user3524402 Jan 09 '17 at 17:48
  • `error_reporting(E_ALL); ini_set('display_errors', '1');` – AbraCadaver Jan 09 '17 at 18:13

1 Answers1

0

The problem is in your test_a() function you feed the array with two references $b and $c, the function however does not accept a reference value and creates a copy instead.

The solution is simple:

function test_a($a, &$b, &$c){
  if($a['a'] == 1){
    $b = true;
    $c++;
  }
}
Xorifelse
  • 7,878
  • 1
  • 27
  • 38