PHP closures (Closure) anonymous functions _php Tutorial

Source: Internet
Author: User

PHP's Closure (Closure) anonymous function


This article mainly introduces the PHP anonymous function introduced by php5.3, that is, the closure (Closure), and the role of closures, very detailed, recommended to the needs of small partners.

PHP's Closure (Closure) is an anonymous function, introduced by PHP5.3.

The syntax of a closure is simple, and the key word to note is that only use,use are connected to closures and external variables.

The code is as follows:


$a = function () use ($b) {}

A simple example is as follows:

The code is as follows:


function callback ($fun) {
$fun ();
}
$msg = "Hello, everyone";
$fun = function () use ($msg) {
Print "This was a closure use string value, MSG is: $msg.
/n ";
};
$msg = "Hello, everybody";
Callback ($fun);

The result: This is a closure use string value, msg Is:hello, everyone.
/n

In the new open closure syntax for PHP, we used use to define variables outside the 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 is determined when the closure is created by the underlying type parameter passed as a pass-through value.

Small applications are as follows:

The code is as follows:


/**
* A counter generator that uses closures
* This is actually a reference to Python in the introduction of closures when the example ...
* We can consider this:
* 1. Each time the counter function is called, 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 created closure and destroys the local variable, but at this point there is a reference to the $counter by the closure.
* It's not going to be recycled, so we can understand that the closure, returned by the function counter, carries a free State
Variable.
* 4. Since each call to counter creates separate $counter and closures, the returned closures are independent of each other.
* 5. Executes the returned closure, and the Free State variable it carries is self-incremented and returned, resulting in a counter.
* Conclusion: This function can be used to generate counters that are independent of each other.
*/
function counter () {
$counter = 1;
return function () use (& $counter) {return $counter + +;};
}
$counter 1 = counter ();
$counter 2 = counter ();
echo "Counter1:". $counter 1 (). "
/n ";
echo "Counter1:". $counter 1 (). "
/n ";
echo "Counter1:". $counter 1 (). "
/n ";
echo "Counter1:". $counter 1 (). "
/n ";
echo "Counter2:". $counter 2 (). "
/n ";
echo "Counter2:". $counter 2 (). "
/n ";
echo "Counter2:". $counter 2 (). "
/n ";
echo "Counter2:". $counter 2 (). "
/n ";
?>

The role of closures

1. Reduce the code of the Foreach Loop
such as the example cart in the Handbook http://php.net/manual/en/functions.anonymous.php

The code is as follows:


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->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 a user-defined function to do callback processing for each element in the array
Array_walk ($this->products, $callback);
Return round ($total, 2);;
}
}
$my _cart = new cart;
Add items to your 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 foreach.

2. Reduce the parameters of the function

The code is as follows:


function html ($code, $id = "", $class = "") {
if ($id!== "") $id = "id = \" $id \ "";
$class = ($class!== "")? "class =\" $class \ ">": ">";
$open = "< $code $id$class";
$close = " ";
return function ($inner = "") Use ($open, $close) {
Return "$open $inner$close";
};
}

If you use the usual method, we will put inner into the HTML function parameters, so that neither code reading nor use is as good as using closures.

3. Lifting the recursive function

The code is as follows:


$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, and the error fib (n-1) is not found in this case (the type of FIB is not defined previously).

So when you want to use closures to remove the loop function, you need to use the

The code is as follows:


$recursive = function () use (& $recursive) {
The function is now available as $recursive
}

Such a form.

4. 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.

The code is as follows:


$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

Small partners on the anonymous function of PHP is the closure function has a new understanding of it, I hope this article can give you some hints, I hope you can like.

http://www.bkjia.com/PHPjc/966918.html www.bkjia.com true http://www.bkjia.com/PHPjc/966918.html techarticle php Closure (Closure) anonymous function Details This paper mainly introduces the introduction of php5.3 PHP anonymous function, that is, the closure (Closure), as well as the role of closures, very detailed, this ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.