Sirwan Afifi Sirwan Afifi - 1 month ago 9
PHP Question

Weird closure behavior in PHP

Given this code in JavaScript:

function getFunc(){
var myVar = 1;
function inc(var1) {
myVar = myVar + 1;
return var1 + myVar;
}
return inc;
}
var inc = getFunc();
console.log(inc(5));
console.log(inc(6));


If you run this code in your browser console it gives you 7 and 9 as the result. I wrote the same code in C# and it gave me the same result:

static void Main(string[] args)
{
var inc = GetAFunc();
Console.WriteLine(inc(5));
Console.WriteLine(inc(6));

Console.ReadKey();
}

public static Func<int, int> GetAFunc()
{
var myVar = 1;
Func<int, int> inc = delegate(int var1)
{
myVar = myVar + 1;
return var1 + myVar;
};
return inc;
}


But why doesn't it give the same result in PHP?

function getFunc() {
$myVar = 1;

$inc = function($var1) use ($myVar) {
$myVar = $myVar + 1;
return $var1 + $myVar;
};

return $inc;
}

$inc = getFunc();
echo $inc(5);
echo $inc(6);


It gave me 7 and 8!

Why they don't work the same way? Is something wrong with the code?

Answer

This is due to the fact you need to be explicit in PHP if you'd like to modify a variable in another scope. You can do this by defining $var as a reference with &. For example: use (&$var).

The following code will give you your expected behaviour:

function test() {
  $var = 1;

  $inc = function($num) use (&$var) {
      var_dump($var);
    $var = $var + 1;
    return $num + $var;
  };

  return $inc;
}

$test = test();
echo $test(5);
echo $test(6);

Hope this helps :) x

Comments