AOP-静态代理
接口类
<?php namespace App\Interfaces; interface ProductInterface { public function create($name); public function edit($name); }
目标类
<?php namespace App\Interfaces\Targets; use App\Interfaces\ProductInterface; class Product implements ProductInterface { public function create($name) { // TODO: Implement create() method. echo "目标类创建"; } public function edit($name) { // TODO: Implement edit() method. echo "目标类编辑"; } }
代理类
<?php namespace App\Interfaces\Proxy; use App\Interfaces\ProductInterface; use App\Interfaces\Targets\Product; class ProductProxy implements ProductInterface { private $product; /** * ProductProxy constructor. */ public function __construct(Product $product) { $this->product = $product; } public function create($name) { // TODO: Implement create() method. echo "befare\n"; $this->product->create($name); echo "after\n"; } public function edit($name) { // TODO: Implement edit() method. echo "befare\n"; $this->product->edit($name); echo "after\n"; } }
消费者
/** * @RequestMapping(route="product/test",method={RequestMethod::GET}) */ public function test(){ $mb_obj = new \App\Interfaces\Targets\Product(); $pdt_obj = new ProductProxy($mb_obj); $pdt_obj->create('张三'); }