Home >php教程 >php手册 >[Strengthening the PHP series] Shopping cart code explains PHP's anonymous functions

[Strengthening the PHP series] Shopping cart code explains PHP's anonymous functions

WBOY
WBOYOriginal
2016-09-23 03:30:081124browse

1. Definition: Anonymous functions, also called closure functions (closures), allow to temporarily create a function without a specified name. The value most commonly used as a callback function argument. Of course, there are other applications as well.

2. Usage:

 1) as the value of the variable :

   Closure functions can also be used as the value of variables. PHP will automatically convert this expression into an object instance of the built-in class Closure. The method of assigning a closure object to a variable is the same as the syntax of ordinary variable assignment, and a semicolon must be added at the end

 2)Inherit variables from parent scope:

   Closures can inherit variables from the parent scope. Any such variables should be passed in using the use language construct.

  3) A complete example, illustrated with shopping cart code:

<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. Reference:

 1) PHP official description of 'anonymous function': http://www.php.net/manual/zh/functions.anonymous.php

 2) The constant() function returns a constant value: http://www.runoob.com/php/func-misc-constant.html

 3) The array_walk() function applies a user-defined function to each element in the array: http://www.w3school.com.cn/php/func_array_walk.asp

 4) The round() function rounds floating point numbers: http://www.w3school.com.cn/php/func_math_round.asp

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn