示例#1
0
    {
        $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();
示例#2
0
$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 />';
echo 'sender2->foo()<br />';
$sender2->foo();
// This observer will receive the two pending notifications on level 3
echo '<br />';
echo 'dispatcher3->addObserver()<br />';
$dispatcher3->addObserver('receiver3', 'onFoo');
// remove one level
$success = $dispatcher1->removeNestedDispatcher($dispatcher2);
if ($success === true) {
    echo '<br />';
    echo 'removed nested dispatcher2 from dispatcher1<br />';
}
// this will stay in level 1
echo 'sender1->foo()<br />';
$sender1->foo();
// this will bubble up from 2-3
echo '<br />';