Maison > php教程 > php手册 > 【夯实PHP系列】购物车代码说明PHP的匿名函数

【夯实PHP系列】购物车代码说明PHP的匿名函数

WBOY
Libérer: 2016-09-23 03:30:08
original
1136 Les gens l'ont consulté

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> ?>
Copier après la connexion

 

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

 

source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal