Location: PHPKode > scripts > Observerable > observerable/observerable.php
<?php

/**
 * @package default
 * @author Michal "Techi" Vrchota <hide@address.com>
 * @license http://www.gnu.org/copyleft/lgpl.html LGPL
 */

/**
 * A class can implement the Observer interface when it wants to be informed of changes in observable objects.
 *
 * @package default
 */

interface Observer
{
	/**
	 * This method is called whenever the observed object is changed.
	 *
	 * @param Observerable $o the observable object.
	 */
	
	public function update(Observerable $o);
}

/**
 * This class represents an observable object, or "data" in the model-view paradigm. It can be subclassed to represent an object that the application wants to have observed. 
 *
 * An observable object can have one or more observers. An observer may be any object that implements interface Observer. After an observable instance changes, an application calling the Observable's notifyObservers method causes all of its observers to be notified of the change by a call to their update method. 
 *
 * The order in which notifications will be delivered is unspecified. The default implementation provided in the Observerable class will notify Observers in the order in which they registered interest, but subclasses may change this order, use no guaranteed order, deliver notifications on separate threads, or may guarantee that their subclass follows this order, as they choose.
 * 
 * @package default  
 */ 

class Observerable
{

	/**
	 * @var array $observers list of observer objects
	 */	 	

	protected $observers = array();
	
	/**
	 * Construct an Observable with zero Observers.
	 * 
	 */	 	
	
	public function __construct()
	{
	
	}
	
	/**
	 * Adds an observer to the set of observers for this object, provided that it is not the same as some observer already in the set.
	 * 
	 * @param Observer $o an observer to be added.
	 */	 	           	 	
	
	public function addObserver(Observer $o)
	{
		$this->observers[] = $o;
	}
	
	/**
	 * Deletes an observer from the set of observers of this object.
	 *
	 * @param Observer $o the observer to be deleted.
	 */
	
	public function deleteObserver(Observer $o)
	{
		$this->observers = array_diff($this->observers, array($o));
	}
	
	/**
	 * Clears the observer list so that this object no longer has any observers.
	 *
	 */
	
	public function deleteObservers()
	{
		unset($this->observers);
		$this->observers = array();
	}
	
	/**
	 * If this object has changed, as indicated by the hasChanged method, then notify all of its observers and then call the clearChanged method to indicate that this object has no longer changed.
	 *
	 */
	
	public function notifyObservers()
	{
		foreach($this->observers as $o)
		{
			$o->update($this);		
		}
	}
	
	/**
	 * Returns the number of observers of this Observable object.
	 *
	 * @return integer the number of observers of this object.
	 */	 	 	 	
	
	public function countObservers()
	{
		return count($this->observers);
	}

}

?>
Return current item: Observerable