82

Possible Duplicate:
In Php 5.3.0 what is the Function “Use” Identifier ? Should a sane programmer use it?

I've been examining the Closures in PHP and this is what took my attention:

public function getTotal($tax)
    {
        $total = 0.00;

        $callback =
            function ($quantity, $product) use ($tax, &$total)
            {
                $pricePerItem = constant(__CLASS__ . "::PRICE_" .
                    strtoupper($product));
                $total += ($pricePerItem * $quantity) * ($tax + 1.0);
            };

        array_walk($this->products, $callback);
        return round($total, 2);
    }

And somebody please give me an explanation about the usage of use in this code.

function ($quantity, $product) use ($tax, &$total)

When I search use in PHP, it finds use keyword where it is used in namespaces but here it looks different.

Thanks.

Community
  • 1
  • 1
Tarik
  • 79,711
  • 83
  • 236
  • 349
  • 3
    Exactly same question [here](http://stackoverflow.com/questions/1065188/in-php-5-3-0-what-is-the-function-use-identifier-should-a-sane-programmer-use). – shinkou Jun 12 '11 at 06:31
  • 1
    Commenting `/* use ($tax, &$total) */` would have answered the question. Dare to explore :) – Petruza Sep 30 '15 at 14:13

3 Answers3

122

The use of "use" is correct in this case too.

With closures, to access variables that are outside of the context of the function you need to explicitly grant permission to the function using the use function. What it means in this case is that you're granting the function access to the $tax and $total variables.

You'll noticed that $tax was passed as a parameter of the getTotal function while $total was set just above the line where the closure is defined.

Another thing to point out is that $tax is passed as a copy while $total is passed by reference (by appending the & sign in front). Passing by reference allows the closure to modify the value of the variable. Any changes to the value of $tax in this case will only be effective within the closure while the real value of $total.

Francois Deschenes
  • 24,816
  • 4
  • 64
  • 61
  • 6
    Thanks for pointing out the pass-by-reference part. I didn't notice it at all.. – Tarik Jun 12 '11 at 14:52
  • 1
    To add. Arrays are also passed by copy. But in PHP it is lazy copy. This means that it is only copied if you start making changes to it. Point, you don't have to worry about performance. – Michael Sep 21 '16 at 02:53
  • Hi, Thanks for your explanation. But I have a little query that we can also pass the variables defined outside the scope as argument to closure. then why we use "use" keyword to do the same thing. – Dhirender Mar 01 '17 at 07:31
18

When you declare an anonymous function in PHP you need to tell it which variables from surrounding scopes (if any) it should close over — they don't automatically close over any in-scope lexical variables that are mentioned in the function body. The list after use is simply the list of variables to close over.

hobbs
  • 223,387
  • 19
  • 210
  • 288
4

This means your inner function can use variables $tax and $total from the outer function, not only its parameters.

Yuri Stuken
  • 12,820
  • 1
  • 26
  • 23