PHP手冊中的匿名函數(shù)怎么用-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計制作、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的豐澤網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

匿名函數(shù)

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

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

<?php
//array_reduce 將回調(diào)函數(shù) callback 迭代地作用到 array 數(shù)組中的每一個單元中,從而將數(shù)組簡化為單一的值。
$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ù)對數(shù)組中的每個元素做回調(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. 實際業(yè)務(wù)用法

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

以上是PHP手冊中的匿名函數(shù)怎么用的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

文章名稱:PHP手冊中的匿名函數(shù)怎么用-創(chuàng)新互聯(lián)
文章出自:http://muchs.cn/article36/cspdpg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、Google、外貿(mào)建站商城網(wǎng)站、靜態(tài)網(wǎng)站、用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)