溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP手冊(cè)中的匿名函數(shù)怎么用

發(fā)布時(shí)間:2020-10-10 16:58:23 來源:億速云 閱讀:105 作者:小新 欄目:編程語言

小編給大家分享一下PHP手冊(cè)中的匿名函數(shù)怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

匿名函數(shù)

匿名函數(shù) 也叫 閉包函數(shù) (closures),可以創(chuàng)建一個(gè)沒有指定名稱的函數(shù),一般作用于回調(diào)函數(shù) (callback) 參數(shù)的值。匿名函數(shù)目前是通過 Closure 類來實(shí)現(xiàn)的。

1. 我們平時(shí)可能用到的相關(guān)函數(shù)舉例

<?php
//array_reduce 將回調(diào)函數(shù) callback 迭代地作用到 array 數(shù)組中的每一個(gè)單元中,從而將數(shù)組簡(jiǎn)化為單一的值。
$array = [1, 2, 3, 4];
$str = array_reduce($array, function ($return_str, $value) {
    $return_str = $return_str . $value;  //層層迭代
    return $return_str;
});
//1.第一次迭代  $return_str = '',value = '1' 返回 '1'
//2.第二次迭代  $return_str = '1',value = '2'  返回 '12'
//3.第三次迭代  $return_str = '12',value = '3'  返回 '123'
//4.第四次迭代  $return_str = '123',value = '4'  返回 '1243'
var_dump($str);
// string('12345')
// array_walk — 使用用戶自定義函數(shù)對(duì)數(shù)組中的每個(gè)元素做回調(diào)處理 
$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, 'test_print');
array_walk($fruits, 'test_alter', 'fruit');
echo "... and after:\n";
array_walk($fruits, 'test_print');
?>

2. 實(shí)際業(yè)務(wù)用法

<?php
// 一個(gè)基本的購物車,包括一些已經(jīng)添加的商品和每種商品的數(shù)量。
// 其中有一個(gè)方法用來計(jì)算購物車中所有商品的總價(jià)格,該方法使
// 用了一個(gè) closure 作為回調(diào)函數(shù)。
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)
            {
                //定義一個(gè)回調(diào)函數(shù) 取出 當(dāng)前商品的價(jià)格
                $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;
// 往購物車?yán)锾砑訔l目
$my_cart->add('butter', 1);
$my_cart->add('milk', 3);
$my_cart->add('eggs', 6);
// 打出出總價(jià)格,其中有 5% 的銷售稅.
print $my_cart->getTotal(0.05) . "\n";
// 最后結(jié)果是 54.29
?>

以上是PHP手冊(cè)中的匿名函數(shù)怎么用的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI