Unauthorized agent and apparent agent Minor6 agent

WBOY
Release: 2016-07-28 08:29:55
Original
1109 people have browsed it

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

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

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

The above has introduced the unauthorized proxy and apparent proxy Minor6 proxy, including the content of unauthorized proxy and apparent proxy. I hope it will be helpful to friends who are interested in PHP tutorials.

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!