时间:2021-07-01 10:21:17 帮助过:14人阅读
Minor提供了一个类似于java中InvocationHandler接口和一个Proxy类的代理模式的实现,具体可参考我的这篇文章:http://www.cnblogs.com/orlion/p/5350752.html
6.1.1 使用
class FooController extends Controller { publicfunction bar($productName) { $log = new LogHandler(); $shop = new Shop(); $shopProxy = Proxy::newProxyInstance($shop, $log); $shopProxy->buy($productName); } }
php namespace App\Lib; use Minor\Proxy\InvocationHandler; class LogHandler implements InvocationHandler { publicfunction invoke($target, \ReflectionMethod $method, Array$args = []) { $this->before(); $result = $method->invokeArgs($target, $args); $this->after(); return$result; } publicfunction before() { echo '[LogHandler] before
'; } publicfunction after() { echo '[LogHandler] after
'; } }
php namespace App\Lib; class Shop { private$mail = null; publicfunction boot(MailProvider $mail) { $this->mail = $mail; } publicfunction buy($productName) { echo '[Shop] buy ' . $productName . '
'; !is_null($this->mail) && $this->mail->send('DemoUser'); } }
以上就介绍了无权代理与表见代理 Minor6代理,包括了无权代理与表见代理方面的内容,希望对PHP教程有兴趣的朋友有所帮助。