PHP中闭包的使用

WBOY
发布: 2016-07-29 09:14:47
原创
911 人浏览过

PHP中闭包的使用

例子一

<code><span><?php </span><span>/**
 * 代码中有一个Di容器用来保存<strong>对象</strong>实例,然后通过set()方法注册服务,通过get()方法获取服务。
 * 我们看到$di->set()的时候,使用了匿名函数,我们预先注册了zhangsan和lisi两个服务,这两个服务都是User类的实例,
 * 在$di->set的时候实际上并没有实例化,而是在$di->get()的时候才执行了匿名函数并将<strong>对象</strong>返回,
 * 这就实现了**按需实例化,不用则不实例化,提高效率**。
 */</span><span><span>class</span><span>Di</span> {</span><span>private</span><span>$_factory</span>;

    <span>public</span><span><span>function</span><span>set</span><span>(<span>$id</span>, <span>$value</span>)</span> {</span><span>$this</span>->_factory[<span>$id</span>] = <span>$value</span>;
    }

    <span>public</span><span><span>function</span><span>get</span><span>(<span>$id</span>)</span> {</span><span>$value</span> = <span>$this</span>->_factory[<span>$id</span>];
        <span>return</span><span>$value</span>();
    }

}

<span><span>class</span><span>User</span> {</span><span>private</span><span>$_username</span>;

    <span><span>function</span><span>__construct</span><span>(<span>$username</span> = <span>""</span>)</span> {</span><span>$this</span>->_username = <span>$username</span>;
    }

    <span><span>function</span><span>getUserName</span><span>()</span> {</span><span>return</span><span>$this</span>->_username;
    }

}

<span>//从这里开始看</span><span>$di</span> = <span>new</span> Di();
<span>$di</span>->set(<span>"zhangsan"</span>, <span><span>function</span><span>()</span> {</span><span>return</span><span>new</span> User(<span>'张三'</span>);
});
<span>$di</span>->set(<span>"lisi"</span>, <span><span>function</span><span>()</span> {</span><span>return</span><span>new</span> User(<span>"李四"</span>);
});
<span>echo</span><span>$di</span>->get(<span>"zhangsan"</span>)->getUserName();
<span>echo</span><span>$di</span>->get(<span>"lisi"</span>)->getUserName();</span></code>
登录后复制

例子二

<code><span>/**
 * 一个基本的购物车,包括一些已经添加的商品和每种商品的数量。
 * 其中有一个方法用来计算购物车中所有商品的总价格。该方法使用了一个closure作为回调函数。
 */</span><span><span>class</span><span>Cart</span> {</span><span>const</span> PRICE_BUTTER = <span>1.00</span>;
    <span>const</span> PRICE_MILK = <span>3.04</span>;
    <span>const</span> PRICE_EGGS = <span>6.95</span>;

    <span>protected</span><span>$products</span> = <span>array</span>();

    <span>public</span><span><span>function</span><span>add</span><span>(<span>$product</span>, <span>$quantity</span>)</span> {</span><span>$this</span>->products[<span>$product</span>] = <span>$quantity</span>;
    }

    <span>public</span><span><span>function</span><span>getQuantity</span><span>(<span>$product</span>)</span> {</span><span>return</span><span>isset</span>(<span>$this</span>->products[<span>$product</span>]) ? <span>$this</span>->products[<span>$product</span>] : <span>FALSE</span>;
    }

    <span>public</span><span><span>function</span><span>getTotal</span><span>(<span>$tax</span>)</span> {</span><span>$total</span> = <span>0.00</span>;

        <span>$callback</span> = <span><span>function</span><span>(<span>$quantity</span>, <span>$product</span>)</span><span>use</span><span>(<span>$tax</span>, &<span>$total</span>)</span> {</span><span>$pricePerItem</span> = constant(<span>__CLASS__</span> . <span>"::PRICE_"</span> . strtoupper(<span>$product</span>));
            <span>$total</span> += (<span>$pricePerItem</span> * <span>$quantity</span>) * (<span>$tax</span> + <span>1.0</span>);
        };

        array_walk(<span>$this</span>->products, <span>$callback</span>);
        <span>return</span> round(<span>$total</span>, <span>2</span>);
    }

}

<span>$my_cart</span> = <span>new</span> Cart;

<span>// 往购物车里添加条目</span><span>$my_cart</span>->add(<span>'butter'</span>, <span>1</span>);
<span>$my_cart</span>->add(<span>'milk'</span>, <span>3</span>);
<span>$my_cart</span>->add(<span>'eggs'</span>, <span>6</span>);

<span>// 打出出总价格,其中有 5% 的销售税.</span><span>print</span><span>$my_cart</span>->getTotal(<span>0.05</span>) . <span>"\n"</span>;

<span>// The result is 54.29</span></code>
登录后复制

以上就介绍了PHP中闭包的使用,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板