1. 定義:匿名函數(Anonymous functions),也叫閉包函數(closures),允許 暫時建立一個沒有指定名稱的函數。最常用作回呼函數(callback)參數的值。當然,也有其它所應用的情況。
2. 用法:
1)作為變數的值:
閉包函數也可以當作變數的值來使用。 PHP 會自動把此種表達式轉換成內建類別 Closure 的物件實例。把一個 closure 物件賦值給一個變數的方式與普通變數賦值的語法是一樣的,最後也要加上分號
2)從父作用域繼承變數:
閉包可以從父作用域繼承變數。 任何此類變數都應該用 use 語言結構傳遞進去。
3)一個完整的例子,用購物車代碼來說明:
<span style="color: #008080;"> 1</span> <?<span style="color: #000000;">php </span><span style="color: #008080;"> 2</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 一个基本的购物车,包括一些已经添加的商品和每种商品的数量。 </span><span style="color: #008080;"> 3</span> <span style="color: #008000;">// 其中有一个方法用来计算购物车中所有商品的总价格,该方法使 </span><span style="color: #008080;"> 4</span> <span style="color: #008000;">// 用了一个 closure 作为回调函数。</span> <span style="color: #008080;"> 5</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Cart </span><span style="color: #008080;"> 6</span> <span style="color: #000000;">{ </span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">const</span> PRICE_BUTTER = 1.00<span style="color: #000000;">; </span><span style="color: #008080;"> 8</span> <span style="color: #0000ff;">const</span> PRICE_MILK = 3.00<span style="color: #000000;">; </span><span style="color: #008080;"> 9</span> <span style="color: #0000ff;">const</span> PRICE_EGGS = 6.95<span style="color: #000000;">; </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #0000ff;">protected</span> <span style="color: #800080;">$products</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> add(<span style="color: #800080;">$product</span>, <span style="color: #800080;">$quantity</span><span style="color: #000000;">) </span><span style="color: #008080;">14</span> <span style="color: #000000;"> { </span><span style="color: #008080;">15</span> <span style="color: #800080;">$this</span>->products[<span style="color: #800080;">$product</span>] = <span style="color: #800080;">$quantity</span><span style="color: #000000;">; </span><span style="color: #008080;">16</span> <span style="color: #000000;"> } </span><span style="color: #008080;">17</span> <span style="color: #008080;">18</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getQuantity(<span style="color: #800080;">$product</span><span style="color: #000000;">) </span><span style="color: #008080;">19</span> <span style="color: #000000;"> { </span><span style="color: #008080;">20</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->products[<span style="color: #800080;">$product</span>]) ? <span style="color: #800080;">$this</span>->products[<span style="color: #800080;">$product</span>] : <span style="color: #008080;">21</span> <span style="color: #0000ff;">FALSE</span><span style="color: #000000;">; </span><span style="color: #008080;">22</span> <span style="color: #000000;"> } </span><span style="color: #008080;">23</span> <span style="color: #008080;">24</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> getTotal(<span style="color: #800080;">$tax</span><span style="color: #000000;">) </span><span style="color: #008080;">25</span> <span style="color: #000000;"> { </span><span style="color: #008080;">26</span> <span style="color: #800080;">$total</span> = 0.00<span style="color: #000000;">; </span><span style="color: #008080;">27</span> <span style="color: #008080;">28</span> <span style="color: #800080;">$callback</span> = <span style="color: #008080;">29</span> <span style="color: #0000ff;">function</span> (<span style="color: #800080;">$quantity</span>, <span style="color: #800080;">$product</span>) <span style="color: #0000ff;">use</span> (<span style="color: #800080;">$tax</span>, &<span style="color: #800080;">$total</span><span style="color: #000000;">) </span><span style="color: #008080;">30</span> <span style="color: #000000;"> { </span><span style="color: #008080;">31</span> <span style="color: #800080;">$pricePerItem</span> = <span style="color: #008080;">constant</span>(<span style="color: #ff00ff;">__CLASS__</span> . "::PRICE_" . <span style="color: #008080;">32</span> <span style="color: #008080;">strtoupper</span>(<span style="color: #800080;">$product</span><span style="color: #000000;">)); </span><span style="color: #008080;">33</span> <span style="color: #800080;">$total</span> += (<span style="color: #800080;">$pricePerItem</span> * <span style="color: #800080;">$quantity</span>) * (<span style="color: #800080;">$tax</span> + 1.0<span style="color: #000000;">); </span><span style="color: #008080;">34</span> <span style="color: #000000;"> }; </span><span style="color: #008080;">35</span> <span style="color: #008080;">36</span> <span style="color: #008080;">array_walk</span>(<span style="color: #800080;">$this</span>->products, <span style="color: #800080;">$callback</span><span style="color: #000000;">); </span><span style="color: #008080;">37</span> <span style="color: #0000ff;">return</span> <span style="color: #008080;">round</span>(<span style="color: #800080;">$total</span>, 2<span style="color: #000000;">);; </span><span style="color: #008080;">38</span> <span style="color: #000000;"> } </span><span style="color: #008080;">39</span> <span style="color: #000000;">} </span><span style="color: #008080;">40</span> <span style="color: #008080;">41</span> <span style="color: #800080;">$my_cart</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Cart; </span><span style="color: #008080;">42</span> <span style="color: #008080;">43</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 往购物车里添加条目</span> <span style="color: #008080;">44</span> <span style="color: #800080;">$my_cart</span>->add('butter', 1<span style="color: #000000;">); </span><span style="color: #008080;">45</span> <span style="color: #800080;">$my_cart</span>->add('milk', 3<span style="color: #000000;">); </span><span style="color: #008080;">46</span> <span style="color: #800080;">$my_cart</span>->add('eggs', 6<span style="color: #000000;">); </span><span style="color: #008080;">47</span> <span style="color: #008080;">48</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 打出出总价格,其中有 5% 的销售税.</span> <span style="color: #008080;">49</span> <span style="color: #0000ff;">print</span> <span style="color: #800080;">$my_cart</span>->getTotal(0.05) . "\n"<span style="color: #000000;">; </span><span style="color: #008080;">50</span> <span style="color: #008000;">//</span><span style="color: #008000;"> 最后结果是 54.29</span> <span style="color: #008080;">51</span> ?>
3. 參考:
1) php官方說明 '匿名函數':http://www.php.net/manual/zh/functions.anonymous.php
2)constant() 函數傳回一個常數的值: http://www.runoob.com/php/func-misc-constant.html
3) array_walk() 函數對陣列中的每個元素套用使用者自訂函數: http://www.w3school.com.cn/php/func_array_walk.asp
4) round() 函數將浮點數四捨五入: http://www.w3school.com.cn/php/func_math_round.asp