Event_Dispatcher::addObserver PHP Méthode

addObserver() public méthode

This method registers a {@link http://www.php.net/manual/en/language.pseudo-types.php#language.types.callback callback} which is called when the notification corresponding to the criteria given at registration time is posted. The criteria are the notification name and eventually the class of the object posted with the notification. If there are any pending notifications corresponding to the criteria given here, the callback will be called straight away. If the notification name is empty, the observer will receive all the posted notifications. Same goes for the class name.
public addObserver ( $callback, $nName = EVENT_DISPATCHER_GLOBAL, $class = null ) : void
Résultat void
    public function addObserver($callback, $nName = EVENT_DISPATCHER_GLOBAL, $class = null)
    {
        if (is_array($callback)) {
            if (is_object($callback[0])) {
                // Note : PHP4 does not allow correct object comparison so
                // only the class name is used for registration checks.
                $reg = get_class($callback[0]) . '::' . $callback[1];
            } else {
                $reg = $callback[0] . '::' . $callback[1];
            }
        } else {
            $reg = $callback;
        }
        $this->_ro[$nName][$reg] = array('callback' => $callback, 'class' => $class);
        // Post eventual pending notifications for this observer
        if (isset($this->_pending[$nName])) {
            foreach (array_keys($this->_pending[$nName]) as $k) {
                $notification =& $this->_pending[$nName][$k];
                if (!$notification->isNotificationCancelled()) {
                    $objClass = get_class($notification->getNotificationObject());
                    if (empty($class) || strcasecmp($class, $objClass) == 0) {
                        call_user_func_array($callback, array(&$notification));
                        $notification->increaseNotificationCount();
                    }
                }
            }
        }
    }

Usage Example

 /**
  * For the given plugin, add all the observers of this plugin.
  */
 private function addPluginObservers(Piwik_Plugin $plugin)
 {
     $hooks = $plugin->getListHooksRegistered();
     foreach ($hooks as $hookName => $methodToCall) {
         $this->dispatcher->addObserver(array($plugin, $methodToCall), $hookName);
     }
 }
All Usage Examples Of Event_Dispatcher::addObserver