1. 정의: 클로저 함수(클로저)라고도 하는 익명 함수는 지정된 이름 없이 함수의 임시 생성을 허용합니다. 콜백 함수 인수로 가장 일반적으로 사용되는 값입니다. 물론 다른 응용 프로그램도 있습니다.
2. 용도:
1) 변수 값 :
클로저 함수는 변수의 값으로도 사용할 수 있습니다. PHP는 자동으로 이 표현식을 내장 클래스 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