Home > Backend Development > PHP Tutorial > Analysis of anonymous function association usage in PHP manual

Analysis of anonymous function association usage in PHP manual

藏色散人
Release: 2023-04-07 11:58:02
forward
2699 people have browsed it

Analysis of anonymous function association usage in PHP manual

Anonymous function

Anonymous function is also called closure function (closure), which can create an unspecified name Function, generally acts on the value of the callback function (callback) parameter. Anonymous functions are currently implemented through the Closure class.

1. Examples of related functions that we may use in daily life

<?php
//array_reduce 将回调函数 callback 迭代地作用到 array 数组中的每一个单元中,从而将数组简化为单一的值。
$array = [1, 2, 3, 4];
$str = array_reduce($array, function ($return_str, $value) {
    $return_str = $return_str . $value;  //层层迭代
    return $return_str;
});
//1.第一次迭代  $return_str = &#39;&#39;,value = &#39;1&#39; 返回 &#39;1&#39;
//2.第二次迭代  $return_str = &#39;1&#39;,value = &#39;2&#39;  返回 &#39;12&#39;
//3.第三次迭代  $return_str = &#39;12&#39;,value = &#39;3&#39;  返回 &#39;123&#39;
//4.第四次迭代  $return_str = &#39;123&#39;,value = &#39;4&#39;  返回 &#39;1243&#39;
var_dump($str);
// string(&#39;12345&#39;)
// array_walk — 使用用户自定义函数对数组中的每个元素做回调处理 
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
function test_alter(&$item1, $key, $prefix)
{
    $item1 = "$prefix: $item1";
}
function test_print($item2, $key)
{
    echo "$key. $item2<br/>\n";
}
echo "Before ...:\n";
array_walk($fruits, &#39;test_print&#39;);
array_walk($fruits, &#39;test_alter&#39;, &#39;fruit&#39;);
echo "... and after:\n";
array_walk($fruits, &#39;test_print&#39;);
?>
Copy after login

2. Actual business usage

<?php
// 一个基本的购物车,包括一些已经添加的商品和每种商品的数量。
// 其中有一个方法用来计算购物车中所有商品的总价格,该方法使
// 用了一个 closure 作为回调函数。
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__ . "::PRICE_" .
                    strtoupper($product));
                $total += ($pricePerItem * $quantity) * ($tax + 1.0);
            };
        array_walk($this->products, $callback);
        return round($total, 2);;
    }
}
$my_cart = new Cart;
// 往购物车里添加条目
$my_cart->add(&#39;butter&#39;, 1);
$my_cart->add(&#39;milk&#39;, 3);
$my_cart->add(&#39;eggs&#39;, 6);
// 打出出总价格,其中有 5% 的销售税.
print $my_cart->getTotal(0.05) . "\n";
// 最后结果是 54.29
?>
Copy after login

---- The above content is from the official manual and is available for reference

The above is the detailed content of Analysis of anonymous function association usage in PHP manual. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
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