小編給大家分享一下PHP手冊中的匿名函數(shù)怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
在新平等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作專業(yè)公司,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,成都全網(wǎng)營銷推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,新平網(wǎng)站建設(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ù) 取出 當(dāng)前商品的價格 $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); // 打出出總價格,其中有 5% 的銷售稅. print $my_cart->getTotal(0.05) . "\n"; // 最后結(jié)果是 54.29 ?>
以上是PHP手冊中的匿名函數(shù)怎么用的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
名稱欄目:PHP手冊中的匿名函數(shù)怎么用
文章源于:http://m.2m8n56k.cn/article34/jdsppe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、網(wǎng)站維護、面包屑導(dǎo)航、網(wǎng)站制作、定制網(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)