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

Minor【 PHP框架】6.代理

WBOY
Libérer: 2016-06-20 08:42:11
original
1279 Les gens l'ont consulté

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>
Copier après la connexion
<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>
Copier après la connexion
<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>
Copier après la connexion

 

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal