7

I have:

function outside( $limit ) {

$tally = 0;

    return function() use ( $limit, &$tally ) {
        $tally++;

        if( $tally > $limit ) {
            echo "limit has been exceeded";
        }
    };
}

$inside = outside( 2 );
$inside();
$inside();
$inside();

Outputs: limit has been exceeded

My understanding:

  1. on $inside = outside( 2 ); this returns the anonymous function and assigns it to the variable$inside. The anonymous function uses the value of $limit (2) and $tally (0).

  2. function $inside() is called. This increments $tally to 1 The value is remembered somehow and so is $limit. What is the purpose of the ampersand before $tally? I know it's used to create references but in this context it confuses me. How can this closure remember the value of $limit?

Any references to official documentation would help!

2
  • Now there's some interesting code! Not seen that stuff in a while. Commented Oct 7, 2014 at 2:01
  • 2
    I actually protest that this question is a duplicate. While it is very similar, this question focuses more about the variable being passed by reference rather than the usage of the use keyword itself. Commented Oct 7, 2014 at 2:13

2 Answers 2

8

Anonymous functions are actually Closure objects in php. If you add var_dump($invoke) to your code, you'll see this:

object(Closure)#1 (1) {
  ["static"]=>
  array(2) {
    ["limit"]=>
    int(2)
    ["tally"]=>
    int(0)
  }
}

use'd variables are stored in the static array in the closure object. When you invoke the closure, these variables are passed to the function, just like normal arguments. Therefore, if you don't use a reference, they will be passed by copying and any changes to them in the function will have no effect.

Sign up to request clarification or add additional context in comments.

2 Comments

How does the value of $limit persist in subsequent calls, when its not declared as a reference?
Oh ok so since its a static priperty of the closure object the value persist throughout subsequent calls? I think i undersrand better now.
4

The & means you pass the argument by reference and not value. This means you can change the variable inside the function, and it will be remembered outside - not just in that function.

By assigning the function to $inside, you are effectively keeping the reference to the variable intact, so it will be remembered from call to call.

See PHP: Passing by Reference

3 Comments

I thought inside functions the keyword, 'static' was used for 'remembering' purposes, not the ampersand for remembering
Yes, that would work, but the & means you can pass the variable from outside the function, such as a reference to an object, or a global value (which isn't the same variable each time), ie it has to be set to a variable outside the function.
You could also pass a variable by value (without the &) and then return that value from the function and set it to the same variable. That would be functionally the same. But if you needed a function to change two variables (for example) then this becomes more complicated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.