Blogn - 记录个人历史 Ver 3.0.0
http://www.bloggern.com
首页  留言本  注册  用户名: 密码:  
写了一个基于PHP的事件机制
作者:左轻侯   1108字节   点击:8308   回复:281   所属分类:技术随笔
创建时间:2006-07-26 02:22:06   最后修改时间:2006-07-26 02:22:06  
EventUtils:

final class EventArgs {
}

interface EventHandler {
public function Invoke($Sender, EventArgs $E);
}

class Event {
private $EventHandlerList = array ();

public function AddEventHandler(EventHandler $EventHandler) {
$this->EventHandlerList[] = $EventHandler;
}

public function DeleteEventHandler(EventHandler $EventHandler) {
$Index = array_search($EventHandler, $this->EventHanlerList);
array_splice($this->EventHanlerList, $Index, 1);
}

public function Invoke($Sender, EventArgs $E) {
foreach ($this->EventHandlerList as $EventHandler) {
$EventHandler->Invoke($Sender, $E);
}
}

}

Test Case:

class TestEventHandler implements EventHandler {
public function Invoke($Sender, EventArgs $E) {
echo "Hello, ".$Sender;
}
}

function TestEvent() {
$Event = new Event();
$EventHandler = new TestEventHandler();
$Event->AddEventHandler($EventHandler);
$Event->Invoke($Event, new EventArgs());
}

Output:

Hello, Object id #5ok.

支持Multicast
麻烦的是,PHP不支持internal class,这个机制可能还要修改
明天再说
附件:
评论:
来自: 左轻侯   字节:ID:78  发贴时间:2006-07-26 02:22:27  原贴  (内容已经隐藏) 
 

本文允许匿名评论

发表评论:(最长不得超过128KB)
验证码:


您不能对本文发表评论。