首页 > php教程 > php手册 > Minor【 PHP框架】6.代理

Minor【 PHP框架】6.代理

WBOY
发布: 2016-06-20 08:42:11
原创
1280 人浏览过

6.1 代理

  Minor提供了一个类似于java中InvocationHandler接口和一个Proxy类的代理模式的实现,具体可参考我的这篇文章:http://www.cnblogs.com/orlion/p/5350752.html

  6.1.1 使用

  

<span style="color: #0000ff;">class</span> FooController <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Controller
{
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> bar(<span style="color: #800080;">$productName</span><span style="color: #000000;">)
    {
       
        </span><span style="color: #800080;">$log</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> LogHandler();
        </span><span style="color: #800080;">$shop</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Shop();
        </span><span style="color: #800080;">$shopProxy</span> = Proxy::newProxyInstance(<span style="color: #800080;">$shop</span>, <span style="color: #800080;">$log</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$shopProxy</span>->buy(<span style="color: #800080;">$productName</span><span style="color: #000000;">);

    }
}</span>
登录后复制
<span style="color: #000000;">php

namespace App\Lib;

</span><span style="color: #0000ff;">use</span><span style="color: #000000;"> Minor\Proxy\InvocationHandler;

</span><span style="color: #0000ff;">class</span> LogHandler <span style="color: #0000ff;">implements</span><span style="color: #000000;"> InvocationHandler
{
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> invoke(<span style="color: #800080;">$target</span>, \ReflectionMethod <span style="color: #800080;">$method</span>, <span style="color: #0000ff;">Array</span> <span style="color: #800080;">$args</span> =<span style="color: #000000;"> [])
    {
        </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">before();
        </span><span style="color: #800080;">$result</span> = <span style="color: #800080;">$method</span>->invokeArgs(<span style="color: #800080;">$target</span>, <span style="color: #800080;">$args</span><span style="color: #000000;">);
        </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">after();

        </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">;
    }

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> before()
    {
        </span><span style="color: #0000ff;">echo</span> '[LogHandler] before<br><br>'<span style="color: #000000;">;
    }

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> after()
    {
        </span><span style="color: #0000ff;">echo</span> '[LogHandler] after<br><br>'<span style="color: #000000;">;
    }
}</span>
登录后复制
<span style="color: #000000;">php

namespace App\Lib;

</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Shop
{

    </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$mail</span> = <span style="color: #0000ff;">null</span><span style="color: #000000;">;

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> boot(MailProvider <span style="color: #800080;">$mail</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$this</span>-><span style="color: #008080;">mail</span> = <span style="color: #800080;">$mail</span><span style="color: #000000;">;
    }

    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> buy(<span style="color: #800080;">$productName</span><span style="color: #000000;">)
    {
        </span><span style="color: #0000ff;">echo</span> '[Shop] buy ' . <span style="color: #800080;">$productName</span> . '<br><br>'<span style="color: #000000;">;
        </span>!<span style="color: #008080;">is_null</span>(<span style="color: #800080;">$this</span>-><span style="color: #008080;">mail</span>) && <span style="color: #800080;">$this</span>-><span style="color: #008080;">mail</span>->send('DemoUser'<span style="color: #000000;">);
    }
}</span>
登录后复制

 

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