Rumah > php教程 > php手册 > teks badan

Minor【 PHP框架】6.代理,minorphp框架代理

WBOY
Lepaskan: 2016-07-06 14:25:08
asal
743 orang telah melayarinya

Minor【 PHP框架】6.代理,minorphp框架代理

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>
Salin selepas log masuk
<?<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>
Salin selepas log masuk
<?<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>
Salin selepas log masuk

 

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan