PHP's Closure (Closure) is also an anonymous function. was introduced by PHP5.3.
The syntax of a closure is simple, and the key word to be aware of is that only use,use means connecting closures and external variables.
[PHP] View plain copy$a =function () use ($b) { }
Several functions of closures:
1 code to reduce the loop of foreach
[PHP] View plain copy<?php//A basic shopping cart, including some already added items and the quantity of each item. There is one way to calculate the total price of all the items in the shopping cart. The method uses a closure as the callback function. Class Cart {Const Price_butter = 1.00; Const PRICE_MILK = 3.00; Const PRICE_EGGS = 6.95; Protected $products =array (); Public function Add ($product, $quantity) {$this->products[$product] = $quantity; } Public Function getquantity ($product) {return isset ($this->products[$product])? $this->produc ts[$product]: FALSE; The Public Function gettotal ($tax) {$total = 0.00; $callback = function ($quantity, $product) use ($tax, & $total) {$pricePerIte m = constant (CLASS. "::P rice_". Strtoupper ($product)); $total + = ($pricePerItem * $quantity) * ($tax + 1.0); }; Array_walk ($this->products, $callback); Return round ($total, 2);; } } $My_cart =new cart; Add entries to the shopping cart $my _cart->add (' Butter ', 1); $my _cart->add (' Milk ', 3); $my _cart->add (' eggs ', 6); Hit the total price, which has a sales tax of 5%. Print $my _cart->gettotal (0.05). "\ n"; The result is 54.29?>
Here, if we transform the Gettotal function, we must use the foreach
2 Reducing the parameters of a function
[PHP] View plain copyfunction html ($code, $id = "", $class = "") { if ($id!== "") $id = "id = \" $id \ ""; $class = ($class!== "")? "class =\" $class \ "": ">"; $open = "< $code $id$class"; $close = "</$code >"; return function ($inner = "") Use ($open, $close) { return "$open $inner$close"};}
If we use the usual method, we will put the inner in the HTML function parameter, so that whether it is code reading or using the closure
3 Lifting the recursive function
[PHP] View plain copy<?php $fib =function ($n) use (& $fib) { if ($n = = 0 | | $n = = 1) return 1; Return $fib ($n-1) + $FIB ($n-2); }; Echo $fib (2). "\ n";//2 $lie = $FIB; $fib =function () {die (' error ');};/ /rewrite $fib variable echo $lie (5);//Error because $FIB is referenced by closure
Note that the use of the & in the above topic is used, the error n-1 is not used here & will not be found (the type of FIB is not previously defined)
So when you want to use closures to remove the loop function, you need to use the
[PHP] View plain copy<?php $recursive =function () use (& $recursive) { //The function was now available as $ Recursive }
Such a form
4 About delay binding
If you need to delay the binding of a variable inside a use, you need a reference, or you will make a copy of it when you define it.
[PHP] View plain copy<?php $result = 0; $one =function () {var_dump ($result);}; $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)
The use of references and non-use of references represents the assignment of a call, or the assignment of a value at the time of declaration
This is the detailed use of the PHP closure function () used () I have compiled for you, and I hope it will be helpful to you in the future.
Related articles:
PHP face Test (Classic)
Design ideas and drawbacks of the PHP namespace
PHP using WebSocket Example