Home  >  Article  >  php教程  >  Minor【 PHP框架】6.代理

Minor【 PHP框架】6.代理

WBOY
WBOYOriginal
2016-06-20 08:42:111258browse

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>

 

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
Previous article:入门2:PHP相关的名词解释Next article:排序算法