[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/joomla/base/ -> observable.php (source)

   1  <?php
   2  /**
   3  * @version        $Id:observer.php 6961 2007-03-15 16:06:53Z tcp $
   4  * @package        Joomla.Framework
   5  * @subpackage    Base
   6  * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7  * @license        GNU/GPL, see LICENSE.php
   8  * Joomla! is free software. This version may have been modified pursuant
   9  * to the GNU General Public License, and as distributed it includes or
  10  * is derivative of works licensed under the GNU General Public License or
  11  * other free or open source software licenses.
  12  * See COPYRIGHT.php for copyright notices and details.
  13  */
  14  
  15  // Check to ensure this file is within the rest of the framework
  16  defined('JPATH_BASE') or die();
  17  
  18  /**
  19   * Abstract observable class to implement the observer design pattern
  20   *
  21   * @abstract
  22   * @package        Joomla.Framework
  23   * @subpackage    Base
  24   * @since        1.5
  25   */
  26  class JObservable extends JObject
  27  {
  28      /**
  29       * An array of Observer objects to notify
  30       *
  31       * @access private
  32       * @var array
  33       */
  34      var $_observers = array();
  35  
  36      /**
  37       * The state of the observable object
  38       *
  39       * @access private
  40       * @var mixed
  41       */
  42      var $_state = null;
  43  
  44  
  45      /**
  46       * Constructor
  47       */
  48  	function __construct() {
  49          $this->_observers = array();
  50      }
  51  
  52      /**
  53       * Get the state of the JObservable object
  54       *
  55       * @access public
  56       * @return mixed The state of the object
  57       * @since 1.5
  58       */
  59  	function getState() {
  60          return $this->_state;
  61      }
  62  
  63      /**
  64       * Update each attached observer object and return an array of their return values
  65       *
  66       * @access public
  67       * @return array Array of return values from the observers
  68       * @since 1.5
  69       */
  70  	function notify()
  71      {
  72          // Iterate through the _observers array
  73          foreach ($this->_observers as $observer) {
  74              $return[] = $observer->update();
  75          }
  76          return $return;
  77      }
  78  
  79      /**
  80       * Attach an observer object
  81       *
  82       * @access public
  83       * @param object $observer An observer object to attach
  84       * @return void
  85       * @since 1.5
  86       */
  87  	function attach( &$observer)
  88      {
  89          // Make sure we haven't already attached this object as an observer
  90          if (is_object($observer))
  91          {
  92              $class = get_class($observer);
  93              foreach ($this->_observers as $check) {
  94                  if (is_a($check, $class)) {
  95                      return;
  96                  }
  97              }
  98              $this->_observers[] =& $observer;
  99          } else {
 100              $this->_observers[] =& $observer;
 101          }
 102      }
 103  
 104      /**
 105       * Detach an observer object
 106       *
 107       * @access public
 108       * @param object $observer An observer object to detach
 109       * @return boolean True if the observer object was detached
 110       * @since 1.5
 111       */
 112  	function detach( $observer)
 113      {
 114          // Initialize variables
 115          $retval = false;
 116  
 117          $key = array_search($observer, $this->_observers);
 118  
 119          if ( $key !== false )
 120          {
 121              unset($this->_observers[$key]);
 122              $retval = true;
 123          }
 124          return $retval;
 125      }
 126  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1