Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

closures

php

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.

Does anybody know about this? Any documentation? And it looks evil, should it ever be used?

like image 901
SeanDowney Avatar asked Jun 30 '09 18:06

SeanDowney


People also ask

What is a closure in PHP?

A closure is an anonymous function that can access variables imported from the outside scope without using any global variables. Theoretically, a closure is a function with some arguments closed (e.g. fixed) by the environment when it is defined. Closures can work around variable scope restrictions in a clean way.

How are closures established in PHP?

Basically a closure in PHP is a function that can be created without a specified name - an anonymous function. Here's a closure function created as the second parameter of array_walk() . By specifying the $v parameter as a reference one can modify each value in the original array through the closure function.

What is the use of anonymous function in PHP?

Anonymous functions, also known as closures , allow the creation of functions which have no specified name. They are most useful as the value of callable parameters, but they have many other uses. Anonymous functions are implemented using the Closure class.

What is closure function in laravel?

A Closure is an anonymous function. Closures are often used as callback methods and can be used as a parameter in a function. If you take the following example: function handle(Closure $closure) { $closure(); } handle(function(){ echo 'Hello! '; });


1 Answers

A simpler answer.

function ($quantity) use ($tax, &$total) { .. };

  1. The closure is a function assigned to a variable, so you can pass it around
  2. A closure is a separate namespace, normally, you can not access variables defined outside of this namespace. There comes the use keyword:
  3. use allows you to access (use) the succeeding variables inside the closure.
  4. use is early binding. That means the variable values are COPIED upon DEFINING the closure. So modifying $tax inside the closure has no external effect, unless it is a pointer, like an object is.
  5. You can pass in variables as pointers like in case of &$total. This way, modifying the value of $total DOES HAVE an external effect, the original variable's value changes.
  6. Variables defined inside the closure are not accessible from outside the closure either.
  7. Closures and functions have the same speed. Yes, you can use them all over your scripts.

As @Mytskine pointed out probably the best in-depth explanation is the RFC for closures. (Upvote him for this.)

like image 127
zupa Avatar answered Oct 13 '22 13:10

zupa