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

Minor【 PHP框架】6.代理

WBOY
WBOY원래의
2016-06-20 08:42:111300검색

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>

 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:入门2:PHP相关的名词解释다음 기사:排序算法