What is the use of php closures?

青灯夜游
Release: 2023-02-26 08:18:01
Original
3198 people have browsed it

What is php closure? What is the use? The following article will introduce you to php closures. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

What is the use of php closures?

What is a php closure?

PHP closure implementation mainly relies on anonymous functions

The anonymous function is passed in as a number in the ordinary function and can also be returned. This achieves a Simple closure.

In layman’s terms: child functions can use local variables in the parent function. This behavior is called closure!

Two holding points of closure:

1. As a reference to a function variable - when the function returns, it is active. ?

2. A Closure is a stack area that does not release resources when a function returns.

In fact, the above two points can be combined into one point, that is, when the closure function returns, the internal variables of the function are activated, and the stack area where the function is located Still retained.

What is the use of php closures?

Implementing PHP closure can not only read the variables inside the function, but also save the variables in the memory all the time, so that the variables will always exist even after the function is executed.

Several functions of closure:

1. Reduce foreach loop code

For example, manual The example in http://php.net/manual/en/functions.anonymous.php is Cart

products[$product] =$quantity;
    }
     
    publicfunction getQuantity($product)
    {
        returnisset($this->products[$product]) ?$this->products[$product] :
               FALSE;
    }
     
    publicfunction getTotal($tax)
    {
        $total= 0.00;
         
        $callback=
            function($quantity,$product) use ($tax, &$total)
            {
                $pricePerItem= constant(__CLASS__. "::PRICE_" .
                    strtoupper($product));
                $total+= ($pricePerItem* $quantity) * ($tax+ 1.0);
            };
         
        array_walk($this->products,$callback);
        returnround($total, 2);;
    }
}
 
$my_cart= new Cart;
 
// 往购物车里添加条目
$my_cart->add('butter', 1);
$my_cart->add('milk', 3);
$my_cart->add('eggs', 6);
 
// 打出出总价格,其中有 5% 的销售税.
print $my_cart->getTotal(0.05) . "\n";
// The result is 54.29
?>
Copy after login

If we transform the getTotal function, we must use foreach

2, Reduce function parameters

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";};
 
}
Copy after login

If we use the usual method, we will put inner into the html function parameters, so that whether it is reading or using the code, it is better to use closures

3. Release the recursive function

Copy after login

Note that use in the above question uses &. If & is not used here, an error will occur n-1) and the function cannot be found (fib was not defined previously) Type)

So when you want to use a closure to cancel a loop function, you need to use a form like

Copy after login

4. About delayed binding

If you need to delay binding the variables in use, you need to use references, otherwise a copy will be made and placed in use when defining

Copy after login

Using references and not using them The reference represents whether the value is assigned when calling or when the function is declared.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of What is the use of php closures?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!