AOP-动态代理
目标类
<?php namespace app\targets; class Goods { public function create($name){ echo "目标类创建:{$name}"; } public function edit($name){ echo "目标类编辑:{$name}"; } }
代理类
namespace app\proxy; class GoodsProxy { private $obj ; /** * GoodsProxy constructor. * @param $obj */ public function __construct($class) { $this->obj = new $class(); } public function __call($name, $arguments) { // TODO: Implement __call() method. $reflection = new \ReflectionClass($this->obj); if($method = $reflection->getMethod($name)){ if($method->isPublic() && !$method->isAbstract()){ $method->invoke($this->obj,$arguments[0]); } } } }
消费者
public function hello() { //** @var Goods $proxy */ /*$proxy = new ProductProxy(new Product()); $proxy->create("张三");*/ /** @var Goods $proxy */ $proxy = new GoodsProxy(Goods::class); $proxy->create("张三"); $proxy->edit("张三2"); }