简单的php aop

本文介绍了一种使用PHP实现的简易AOP(面向切面编程)方法。通过包装业务逻辑类,实现了权限检查与日志记录的功能。此方法相对Spring 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();
}
?>

屏幕显示:

权限检查
业务逻辑
日志记录


http://blog.163.com/lgh_2002/blog/static/44017526201052563459/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值