Home > php教程 > php手册 > body text

Minor【 PHP框架】6.代理

WBOY
Release: 2016-06-20 08:42:11
Original
1259 people have browsed it

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>
Copy after login
<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>
Copy after login
<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>
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template