Home > Article > Backend Development > 容易的php aop
简单的php aop
对比之下,感觉比spring aop简单易懂。
<?php //应用程序中某个业务逻辑类 class BIZ { public function foobar() { echo '业务逻辑<br />'; } } //业务逻辑类的包装类 class AOP { private $instance; public function __construct($instance) { $this->instance = $instance; } public function __call($method, $argument) { if(! method_exists($this->instance, $method)) { throw new Exception('未定义的方法:' . $method); } echo '权限检查<br />'; $callBack = array($this->instance, $method); $return = call_user_func_array($callBack, $argument); echo '日志记录<br />'; return $return; } } //工厂方法 class Factory { public function getBizInstance() { return new AOP(new BIZ()); } } //客户端调用演示 header("Content-Type: text/html; charset=gbk"); try { $obj = Factory::getBizInstance(); $obj->foobar(); } catch(Exception $e) { echo 'Caught exception: ', $e->getMessage(); } ?> 屏幕显示: 权限检查 业务逻辑 日志记录