Event_Dispatcher::getInstance PHP Method

getInstance() public method

There is usually no need to have more than one notification center for an application so this is the recommended way to get a Event_Dispatcher object.
public getInstance ( $name = '__default' ) : object
return object Event_Dispatcher
    function &getInstance($name = '__default')
    {
        static $dispatchers = array();
        if (!isset($dispatchers[$name])) {
            $dispatchers[$name] = new Event_Dispatcher($name);
        }
        return $dispatchers[$name];
    }

Usage Example

Ejemplo n.º 1
0
    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();
All Usage Examples Of Event_Dispatcher::getInstance