Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5. Some parts of this website may be subject to other licenses.
Source code for file /joomla/base/observable.php

Documentation is available at observable.php

  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 - 2008 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.  * @author        Louis Landry <louis.landry@joomla.org>
  23.  * @package        Joomla.Framework
  24.  * @subpackage    Base
  25.  * @since        1.5
  26.  */
  27. class JObservable extends JObject
  28. {
  29.     /**
  30.      * An array of Observer objects to notify
  31.      *
  32.      * @access private
  33.      * @var array 
  34.      */
  35.     var $_observers array();
  36.  
  37.     /**
  38.      * The state of the observable object
  39.      *
  40.      * @access private
  41.      * @var mixed 
  42.      */
  43.     var $_state null;
  44.  
  45.  
  46.     /**
  47.      * Constructor
  48.      */
  49.     function __construct({
  50.         $this->_observers array();
  51.     }
  52.  
  53.     /**
  54.      * Get the state of the JObservable object
  55.      *
  56.      * @access public
  57.      * @return mixed The state of the object
  58.      * @since 1.5
  59.      */
  60.     function getState({
  61.         return $this->_state;
  62.     }
  63.  
  64.     /**
  65.      * Update each attached observer object and return an array of their return values
  66.      *
  67.      * @access public
  68.      * @return array Array of return values from the observers
  69.      * @since 1.5
  70.      */
  71.     function notify()
  72.     {
  73.         // Iterate through the _observers array
  74.         foreach ($this->_observers as $observer{
  75.             $return[$observer->update();
  76.         }
  77.         return $return;
  78.     }
  79.  
  80.     /**
  81.      * Attach an observer object
  82.      *
  83.      * @access public
  84.      * @param object $observer An observer object to attach
  85.      * @return void 
  86.      * @since 1.5
  87.      */
  88.     function attach&$observer)
  89.     {
  90.         // Make sure we haven't already attached this object as an observer
  91.         if (is_object($observer))
  92.         {
  93.             $class get_class($observer);
  94.             foreach ($this->_observers as $check{
  95.                 if (is_a($check$class)) {
  96.                     return;
  97.                 }
  98.             }
  99.             $this->_observers[=$observer;
  100.         else {
  101.             $this->_observers[=$observer;
  102.         }
  103.     }
  104.  
  105.     /**
  106.      * Detach an observer object
  107.      *
  108.      * @access public
  109.      * @param object $observer An observer object to detach
  110.      * @return boolean True if the observer object was detached
  111.      * @since 1.5
  112.      */
  113.     function detach$observer)
  114.     {
  115.         // Initialize variables
  116.         $retval false;
  117.  
  118.         $key array_search($observer$this->_observers);
  119.  
  120.         if $key !== false )
  121.         {
  122.             unset($this->_observers[$key]);
  123.             $retval true;
  124.         }
  125.         return $retval;
  126.     }
  127. }

Documentation generated on Tue, 29 Jan 2008 18:48:43 +0000 by phpDocumentor 1.3.1