实用:JAVA事件模式下PHP如何实现__教程 |
|
日期:2007-5-15 21:03:20 人气:74 [大 中 小] |
|
|
|
$this->listeners->add($listener); }
function work() { echo "Working!\n"; $this->fireMyEvent(); echo "Working!\n"; $this->fireMyEvent(); echo "Working!\n"; }
protected function fireMyEvent() { $event = new EventObject($this); foreach ($this->listeners->getRaw() as $listener) { $listener->onMyEvent($event); } }
function onMyEvent(EventObject $event) { echo "Taking short break...\n"; }
}
$object = new MyClass(); $object->work(); // Will print: // Working! // Taking short break... // Working! // Taking short break... // Working!
使用事件描述模式,可以增进你的代码的稳定性,但是要注意的是他要在你的代码中添加很多接口和事件类,这样比没使用前执行要慢。 |
|
出处:本站原创 作者:佚名 |
|
|