PHP Closure (Closure), anonymous function instance detailed

Source: Internet
Author: User
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 is a closure use string value, MSG is: $msg. <br/&G T;/n ";}; $msg = "Hello, everybody"; callback ($fun);

The result: This is a closure use string value, msg Is:hello, everyone. <BR/>/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:

/** * A counter generator using closures * Here is a reference to Python in the case of closures ... * 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 time there is a $counter reference to the closure, * It is not recycled, so we can understand that the closure returned by the function counter, carrying 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 ().  "<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 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:

<?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->products[$PR    Oduct]: FALSE;        The 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 an entry to the shopping cart $my_cart->aDD (' butter ', 1); $my _cart->add (' Milk ', 3); $my _cart->add (' eggs ', 6);//The Total price, which has 5% sales tax. 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 =\" $clas S\ ">": ">"; $open = "< $code $id$class"; $close = "</$code >"; 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:

<?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, 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:

<?php$recursive = function () use (& $recursive) {//The function was 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:

<?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

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.