0

I have recently go through these examples of anonymous function, but i am not very clear about it, what is the difference between them, why in second function use keyword used and why first one return null and second one return 0:

<?php
$result = 0;

// first function
$one = function()
{ var_dump($result); };

// second function with use
$two = function() use ($result)
{ var_dump($result); };

$three = function() use (&$result)
{ var_dump($result); };


$result++;

$one();    // outputs NULL: $result is not in scope
$two();    // outputs int(0): $result was copied
$three();    // outputs int(1)
?>

Refrence link: http://php.net/manual/en/functions.anonymous.php

Ahmad Hassan
  • 371
  • 4
  • 22
  • 1
    Did you try to run your example? – rndus2r Sep 28 '17 at 19:34
  • 2
    Are notices disabled? If so, activate them for testing purposes. `use` allows you to either reference or use a variable outside of the scope of an anonymous function. That being said, anon funcs have their own scope (including some super globals). This means you cannot access anything outside this scope. So, if you don't declare it, your variable will be used in the scope of the anon func, thus it's `NULL` (if you had turned notices on, you would get a notice for that). Accessing `$result` directly will allow you to **use**, but not modify `$result`. `&$result` passes the reference. – rndus2r Sep 28 '17 at 19:41
  • yes! i tried same result as mention. – Ahmad Hassan Sep 28 '17 at 19:43

3 Answers3

0

Well

    // first function
$one = function()
{ var_dump($result); 
};

must retrurn a Notice Undefined variable... if in php.ini notice is disabled return null

// second function with use
$two = function() use ($result)
{ var_dump($result); };

Return $result = 0

you increment $result++;

$three = function() use (&$result)
{ var_dump($result); };

return 0+1 = 1;

helmis.m
  • 234
  • 1
  • 18
0

This emits a Notice: Undefined variable and returns NULL because $result is not in the scope of the function, just like any function:

// first function
$one = function()
{ var_dump($result); };

This imports the value of $result to the anonymous function. The value at this point is 0 so the function accessed by $two will always use 0 for $result:

// second function with use
$two = function() use ($result)
{ var_dump($result); };

This one uses $result as a reference to the actual $result variable, so when it is incremented before the function call it is 1. This also allows you to change $result from within the function:

$three = function() use (&$result)
{ var_dump($result); };

Another way to do get variables into your function is to define it to accept arguments and pass them in:

$one = function($var)
{ var_dump($var); };

$one($result);

Or with a reference:

$one = function(&$var)
{ var_dump($var); };

$one($result);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

From the link you posted

Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct. From PHP 7.1, these variables must not include superglobals, $this, or variables with the same name as a parameter.

So if you want to use variables from outside the function you pass them through the use construct.

fbg13
  • 156
  • 1
  • 14