Home  >  Article  >  Backend Development  >  Minor [PHP Framework] 6. Proxy, minorphp framework proxy_PHP tutorial

Minor [PHP Framework] 6. Proxy, minorphp framework proxy_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:50:03929browse

Minor【PHP Framework】6. Proxy, minorphp framework proxy

6.1 Proxy

Minor provides an implementation of the proxy mode similar to the InvocationHandler interface in Java and a Proxy class. For details, please refer to my article: http://www.cnblogs.com/orlion/p/5350752.html

 6.1.1 Using

 

<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>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1135178.htmlTechArticleMinor [PHP Framework] 6. Proxy, minorphp framework proxy 6.1 Proxy Minor provides an interface similar to the InvocationHandler in java And the implementation of the proxy mode of a Proxy class, please refer to...
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