getInstance() public méthode

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
Résultat object Event_Dispatcher
Exemple #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();
 private function __construct()
 {
     $this->dispatcher = Event_Dispatcher::getInstance();
 }
Exemple #3
0
 /**
  * Constructor. Use the factory or singleton methods.
  *
  * @param  bool|object $debug   Boolean that indicates if a log instance
  *                              should be created or an instance of a class
  *                              that implements the PEAR:Log interface.
  * @return void
  * @access protected
  * @see    LiveUser::factory
  * @see    LiveUser::singleton
  */
 function LiveUser(&$debug)
 {
     $this->stack =& PEAR_ErrorStack::singleton('LiveUser');
     if ($debug) {
         $log =& LiveUser::PEARLogFactory($debug);
         if ($log) {
             $this->log =& $log;
             $this->stack->setLogger($this->log);
         }
     }
     $this->stack->setErrorMessageTemplate($this->_errorMessages);
     $this->dispatcher =& Event_Dispatcher::getInstance();
 }
 /**
  * Registers a new listener
  *
  * Registers a new listener with the given criteria.
  *
  * @param mixed  $callback A PHP callback
  * @param string $nName    (optional) Expected notification name
  *
  * @access public
  * @return void
  * @since  version 1.8.0b2 (2008-06-03)
  */
 function addListener($callback, $nName = EVENT_DISPATCHER_GLOBAL)
 {
     $this->dispatcher =& Event_Dispatcher::getInstance();
     // $this->dispatcher->setNotificationClass('PHP_CompatInfo_Audit');
     $this->dispatcher->addObserver($callback, $nName);
     $this->_observerCount++;
 }
 /**
  * Attachs a new observer.
  *
  * Adds a new observer to the Event Dispatcher that will listen
  * for all messages emitted by this HTML_Progress2 instance.
  *
  * @param      mixed     $callback      PHP callback that will act as listener
  * @param      string    $nName         Expected notification name, serves as a filter
  *
  * @return     void
  * @since      2.0.0
  * @access     public
  * @throws     HTML_PROGRESS2_ERROR_INVALID_CALLBACK,
  *             HTML_PROGRESS2_ERROR_INVALID_INPUT
  * @see        removeListener()
  */
 function addListener($callback, $nName = EVENT_DISPATCHER_GLOBAL)
 {
     if (!is_callable($callback)) {
         return $this->raiseError(HTML_PROGRESS2_ERROR_INVALID_CALLBACK, 'exception', array('var' => '$callback', 'element' => 'valid Class-Method/Function', 'was' => 'callback', 'paramnum' => 1));
     } elseif (!is_string($nName)) {
         return $this->raiseError(HTML_PROGRESS2_ERROR_INVALID_INPUT, 'exception', array('var' => '$nName', 'was' => gettype($nName), 'expected' => 'string', 'paramnum' => 2));
     }
     $this->dispatcher =& Event_Dispatcher::getInstance('ProgressMeter');
     $this->dispatcher->addObserver($callback, $nName);
     $this->_observerCount++;
 }
 function test2()
 {
     $nf = new Notified();
     $dm = new Dummy();
     $ed2 =& Event_Dispatcher::getInstance('another');
     $ed1 =& Event_Dispatcher::getInstance();
     $ed2->addObserver(array(&$nf, 'notifReceived'));
     $not =& $ed2->post($dm, 'test', array('A', 'B'));
     $this->assertEquals('test:A:B:default', $nf->description(), "Error");
     $this->assertEquals(1, $not->getNotificationCount(), "Error");
     $not =& $ed1->post($dm, 'test', array('A2', 'B2'));
     $this->assertEquals(1, $not->getNotificationCount(), "Error");
     $not =& $ed1->post($dm, 'test', array('A2', 'B2'));
     $this->assertEquals(1, $not->getNotificationCount(), "Error");
     $ed2->addObserver(array(&$nf, 'notifReceived'), 'ClassFilterNotification', 'Notifier');
     $not =& $ed2->post($dm, 'ClassFilterNotification');
     $this->assertEquals('ClassFilterNotification::default', $nf->description(), "Error");
     $this->assertEquals(1, $not->getNotificationCount(), "Error");
     $ed2->addObserver(array(&$nf, 'notifReceived'), 'ClassFilterNotification', 'Dummy');
     $not =& $ed2->post($dm, 'ClassFilterNotification');
     $this->assertEquals(2, $not->getNotificationCount(), "Error");
 }
Exemple #7
0
 /**
  * Attachs a new observer.
  *
  * Adds a new observer to the Event Dispatcher that will listen
  * for all messages emitted by this HTML_Progress2 instance.
  *
  * @param      mixed     $callback      PHP callback that will act as listener
  *
  * @return     void
  * @since      2.0.0
  * @access     public
  * @throws     HTML_PROGRESS2_ERROR_INVALID_CALLBACK
  * @see        removeListener()
  */
 function addListener($callback)
 {
     if (!is_callable($callback)) {
         return $this->raiseError(HTML_PROGRESS2_ERROR_INVALID_CALLBACK, 'exception', array('var' => '$callback', 'element' => 'valid Class-Method/Function', 'was' => 'callback', 'paramnum' => 1));
     }
     $this->dispatcher = Event_Dispatcher::getInstance();
     $this->dispatcher->addObserver($callback);
     $this->_observerCount++;
 }
Exemple #8
0
 */
function receiver2(&$notification)
{
    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 />';