PHP通过__call实现简单的AOP(主事务后的其他操作)比如前置通知,后置通知
2024-10-19 14:48:32
/**
* person class
*/
class Person { /**
* person class -> function say
*/
public static function say($i)
{
echo "hi-->";
var_dump($i);
//return "hi";
} }
class Dog{
public function say()
{
echo "wang !";
}
}
APO的主要思路 (__call魔术方法:调用一个没有的方法时执行)
interface Monitor{
public function __construct($class);
public function __call($name, $arguments);
} abstract class Event{
public abstract function before();
public abstract function after();
} class MonitorBase implements Monitor{
private $classes = null;
public function __construct($class){
$this->classes=$class; }
public function __call($name, $arguments){
echo "前置输出";
echo $name; call_user_func(array($this->classes,$name),$arguments);
echo "后置输出";
}
}
测试 :
$p = new MonitorBase(new Person());
$p->say("888",'666',array(1,2,3,65,4)); $d = new MonitorBase(new Dog());
$d->say();
结果:
最后:细心的读者可能注意到 抽象类Event 没有用到
这个其实可以让 Person 和Dog 继承然后重写 before()和after(),这样每一个类都有自己的前置和后置通知了...
最新文章
- FreeRTOS run on eclipse
- Linux下python升级至2.7
- HDU 3709 Balanced Number
- c语言scanf返回值
- NPOI 读写Excel
- Linux 高可用开源方案 Keepalived VS Heartbeat对比
- AJAX如何接收JSON数据
- inand和emmc区别
- sizeWithFont 不是线程安全。
- zoj 3716
- hdu 1563 Find your present!
- eval &; exec(绕过长度限制思路学习)
- c#发送get请求
- C# ASP.NET 转换为int型的方法 很实用
- 【编程技巧】alert vs Ext.Msg.alert
- poj-1008-玛雅历
- 【转】构建高性能WEB站点之 吞吐率、吞吐量、TPS、性能测试
- tkinter学习系列(二)之窗口的设置
- Django本地开发,引用静态文件,火狐浏览器不能访问静态文件,谷歌浏览器却能访问静态文件
- excel 获取提取数字