LMSouq
php-dev Open

In PHP, what is a closure and why does it use the "use" identifier?

SE
SeanDowney
1 month ago
3 views
Problem Description
I'm checking out some `PHP 5.3.0` features and ran across some code on the site that looks quite funny: public function getTotal($tax) { $total = 0.00; $callback = /* This line here: */ 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); } as one of the examples on [anonymous functions](http://www.php.net/manual/en/functions.anonymous.php). Does anybody know about this? Any documentation? And it looks evil, should it ever be used?

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base