Location: PHPKode > scripts > Observerable > observerable/observerable-spl.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.
 * Since PHP 5.1 SPL library contains SplObserver interface and SplSubject class
 *
 * 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 implements SplSubject
{

	/**
	 * @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 SplObserver $o an observer to be added.
	 */	 	           	 	
	
	public function addObserver(SplObserver $o)
	{
		if(!$this->containObserver($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(SplObserver $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);
	}

	/**
	 * Check if observer already in the list
	 *
	 * @param SplObserver $observer
	 * @return bool
	 */
	
	public function containObserver(SplObserver $observer)
	{
		return in_array($observer, $this->observers);
	}
	
	/**
	 * Add observer
	 *
	 * @param SplObserver $observer
	 */
	
	public function attach(SplObserver $observer)
	{
		$this->addObserver($observer);
	}
	
	/**
	 * Remove observer
	 *
	 * @param SplObserver $observer
	 */
	
	public function detach(SplObserver $observer)
	{
		$this->deleteObserver($observer);
	}
	
	/**
	 * Notify observers
	 *
	 */
	
	public function notify()
	{
		$this->notifyObservers();
	}
}

?>
Return current item: Observerable