PHP's Closure (Closure) is the anonymous function, which is introduced by PHP5.3.
The syntax for closures is simple, and the only use,use that need to be noticed are the connection closures and the external variables.
Copy Code code as follows:
$a = function () use ($b) {}
The simple example is as follows:
Copy Code code as follows:
function callback ($fun) {
$fun ();
}
$msg = "Hello, everyone";
$fun = function () use ($msg) {
print ' This is a closure use string value, and MSG is: $msg. <br/>/n ';
};
$msg = "Hello, everybody";
Callback ($fun);
As a result: this is a closure use string value, msg Is:hello, everyone. <BR/>/n
In the new open closure syntax for PHP, we use using a variable that is defined externally by a closure. Here we use the external variable $msg, after the definition, and then the value of the change, the closure is executed after the output is the original value. The value of the closure use when the closure is created is determined by the underlying type parameter passed in the value-passing mode.
Small applications are as follows:
Copy Code code as follows:
/**
* A counter generator that utilizes closures
* Here is a reference to the example of the closure in Python ...
* We can consider this:
* 1. Each time the counter function is invoked, a local variable $counter is created, initialized to 1.
* 2. Then a closure is created, and the closure produces a reference to the local variable $counter.
* 3. The function counter returns the closure created and destroys the local variable, but at this point there is a $counter reference to the closure.
* It's not going to be recycled, so we can understand that the closure that is returned by the function counter carries a free State
Variable.
* 4. Because each call to counter creates separate $counter and closures, the returned closures are independent of each other.
* 5. The returned closure is performed, and the Free State variable carried by it is added and returned with a counter.
* Conclusion: This function can be used to generate a counter that is independent of each other.
*/
function counter () {
$counter = 1;
return function () use (& $counter) {return $counter + +;};
}
$counter 1 = counter ();
$counter 2 = counter ();
echo "Counter1:". $counter 1 (). "<br/>/n";
echo "Counter1:". $counter 1 (). "<br/>/n";
echo "Counter1:". $counter 1 (). "<br/>/n";
echo "Counter1:". $counter 1 (). "<br/>/n";
echo "Counter2:". $counter 2 (). "<br/>/n";
echo "Counter2:". $counter 2 (). "<br/>/n";
echo "Counter2:". $counter 2 (). "<br/>/n";
echo "Counter2:". $counter 2 (). "<br/>/n";
?>
The function of closure
1. Code to reduce the Foreach loop
such as the manual http://php.net/manual/en/functions.anonymous.php example Cart
Copy Code code as follows:
<?php
A basic shopping cart, including some items that have been added and the quantity of each item.
There is a way to calculate the total price of all items in a shopping cart. The method uses a closure as a 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->products[$product]:
FALSE;
}
Public Function Gettotal ($tax)
{
$total = 0.00;
$callback =
function ($quantity, $product) use ($tax, & $total)
{
$pricePerItem = constant (__class__. "::P rice_".
Strtoupper ($product));
$total + + ($pricePerItem * $quantity) * ($tax + 1.0);
};
Use user-defined functions to do callback for each element in the array
Array_walk ($this->products, $callback);
Return round ($total, 2);
}
}
$my _cart = new cart;
Add entries to your shopping cart
$my _cart->add (' Butter ', 1);
$my _cart->add (' Milk ', 3);
$my _cart->add (' eggs ', 6);
Make a total price, of which there is a 5% sales tax.
Print $my _cart->gettotal (0.05). "\ n";
The result is 54.29
?>
Here if we transform the Gettotal function must be used to foreach.
2. Reduce the parameters of the function
Copy Code code as follows:
function 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 you are using the usual method, we will put inner into the HTML function parameters, so that no matter whether the code read or use the closure.
3. Release recursive function
Copy Code code as follows:
<?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 used in the above question, there is no use of & error FIB (n-1) is unable to find the function (previously not defined FIB type)
So when you want to use the closure to unlock the loop function, you need to use
Copy Code code as follows:
<?php
$recursive = function () use (& $recursive) {
The function is now available as $recursive
}
Such a form.
4. Delayed binding
If you need to delay binding a variable within the use, you need to refer to it, or you will make a copy of it in the application when you define it.
Copy Code code as follows:
<?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 isn't in scope
$two (); outputs int (0): $result was copied
$three (); outputs int (1)
Using a reference and not using a reference represents whether the call is assigned, or when it is declared.
Small partners whether the anonymous function of PHP is the closure function has a new understanding of it, I hope this article can give you some tips, I hope you can enjoy.