function sender(&$dispatcher) { $this->_dispatcher =& $dispatcher; } function foo() { $this->_dispatcher->post($this, 'onFoo', 'Some Info...'); } } /** * example observer */ function receiver1(&$notification) { echo "receiver 1 received notification<br />\n"; // the notification will be cancelled and no other observers // will be notified $notification->cancelNotification(); } /** * example observer */ function receiver2(&$notification) { echo "receiver 2 received notification<br />\n"; } $dispatcher =& Event_Dispatcher::getInstance(); $sender = new sender($dispatcher); $dispatcher->addObserver('receiver1', 'onFoo'); $dispatcher->addObserver('receiver2', 'onFoo'); $sender->foo();
echo "receiver 2 received notification<br />\n"; } /** * example observer */ function receiver3(&$notification) { echo "receiver 3 received notification<br />\n"; } // get the different dispatchers $dispatcher1 =& Event_Dispatcher::getInstance(); $dispatcher2 =& Event_Dispatcher::getInstance('child'); $dispatcher3 =& Event_Dispatcher::getInstance('grandchild'); // create senders in two different levels $sender1 = new sender($dispatcher1); $sender2 = new sender($dispatcher2); // build three levels $dispatcher1->addNestedDispatcher($dispatcher2); $dispatcher2->addNestedDispatcher($dispatcher3); // add observers in level one and two $dispatcher1->addObserver('receiver1', 'onFoo'); $dispatcher2->addObserver('receiver2', 'onFoo'); // this will bubble up from 1 to 3 echo 'sender1->foo()<br />'; $sender1->foo(); // this will not bubble up echo '<br />'; echo 'sender1->foo(), but disable bubbling<br />'; $sender1->foo(false); // this will bubble up from 2 to 3 echo '<br />';