首頁 >後端開發 >php教程 >無權代理與表見代理 Minor6代理

無權代理與表見代理 Minor6代理

WBOY
WBOY原創
2016-07-28 08:29:551151瀏覽

6.1 代理

  Minor提供了一個類似於java中InvocationHandler介面和一個Proxy類的代理模式的實現,具體可參考我的這篇文章:http:// www.cnblogs.com/orlion/p/5350752.html

6.1.1 使用

<span>class</span> FooController <span>extends</span><span> Controller
{
    </span><span>public</span><span>function</span> bar(<span>$productName</span><span>)
    {
       
        </span><span>$log</span> = <span>new</span><span> LogHandler();
        </span><span>$shop</span> = <span>new</span><span> Shop();
        </span><span>$shopProxy</span> = Proxy::newProxyInstance(<span>$shop</span>, <span>$log</span><span>);
        </span><span>$shopProxy</span>->buy(<span>$productName</span><span>);

    }
}</span>

<?<span>php

namespace App\Lib;

</span><span>use</span><span> Minor\Proxy\InvocationHandler;

</span><span>class</span> LogHandler <span>implements</span><span> InvocationHandler
{
    </span><span>public</span><span>function</span> invoke(<span>$target</span>, \ReflectionMethod <span>$method</span>, <span>Array</span><span>$args</span> =<span> [])
    {
        </span><span>$this</span>-><span>before();
        </span><span>$result</span> = <span>$method</span>->invokeArgs(<span>$target</span>, <span>$args</span><span>);
        </span><span>$this</span>-><span>after();

        </span><span>return</span><span>$result</span><span>;
    }

    </span><span>public</span><span>function</span><span> before()
    {
        </span><span>echo</span> '[LogHandler] before<br/><br/>'<span>;
    }

    </span><span>public</span><span>function</span><span> after()
    {
        </span><span>echo</span> '[LogHandler] after<br/><br/>'<span>;
    }
}</span>

<?<span>php

namespace App\Lib;

</span><span>class</span><span> Shop
{

    </span><span>private</span><span>$mail</span> = <span>null</span><span>;

    </span><span>public</span><span>function</span> boot(MailProvider <span>$mail</span><span>)
    {
        </span><span>$this</span>-><span>mail</span> = <span>$mail</span><span>;
    }

    </span><span>public</span><span>function</span> buy(<span>$productName</span><span>)
    {
        </span><span>echo</span> '[Shop] buy ' . <span>$productName</span> . '<br/><br/>'<span>;
        </span>!<span>is_null</span>(<span>$this</span>-><span>mail</span>) && <span>$this</span>-><span>mail</span>->send('DemoUser'<span>);
    }
}</span>

以上就介紹了無權代理與表見代理 Minor6代理,包括了無權代理與表見代理方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn