This is a bit of code I've put together to try to handle errors in a nice and structured way. The idea is to allow disparate parts of an application know when something bad happened, without having to worry about scope issues and without littering the global scope with variables.

The observer pattern seems to fit well for this. The idea is that every class which you want to be aware when an error happens is added to the Observable class. Then when some client code encounters an error, it sets the error in the Observable class and then triggers the notify method on each class 'observing' the Observable.

Code is in PHP 5 but the pattern can be applied in PHP 4.

define( '_ENTRY_POINT_OK', 'Everything was ok' ); // A message to watch for
define( '_ENTRY_POINT_FAILURE', 'Something was wrong' ); // A message to watch for

/**
 *	A container for listeners and some abstract state
 *	notify() method calls notify method on each observer
 *	Implements Singleton pattern
 */
class ErrorObservable
{
	/**
	 *	Singleton container for class
	 *	@var object $instance
	 *	@access private
	 */
	private static $_instance;
	/**
	 *	Container for some abstract state
	 *	@var mixed $state
	 *	@access private
	 */
	private $_state;
	/**
	 *	Container for each observer requesting notification
	 *	@var array $_observers
	 *	@access private
	 */
	private $_observers = array();
	/**
	 *	addObserver()
	 *	Adds a correctly typed observer to the array
	 *	@access public
	 *	@param object $notable
	 *	@return void
	 */
	public function addObserver( IError $observer)
	{
		$this->_observers[] = $observer;
	}
	/**
	 *	notify()
	 *	Triggers a method on each observer in turn
	 *	@access public
	 *	@return void
	 */
	public function notify()
	{
		foreach( $this->_observers as $l )
		{
			$l->notify( $this->getState() );
		}
	}
	/**
	 *	getState()
	 *	Returns the abstract state
	 *	@access private
	 *	@return mixed $this->state
	 */
	private function getState()
	{
		return $this->_state;
	}
	/**
	 *	setState()
	 *	Sets the state
	 *	@access public
	 *	@param mixed $state
	 *	@return void
	 */
	public function setState( $state )
	{
		$this->_state = $state;
	}
	/**
	 *	getInstance()
	 *	Singleton implementation
	 *	@access public
	 *	@mod static
	 *	@return object $this->instance
	 */
	public static function getInstance()
	{
		if(!is_a( ErrorObservable::$_instance, "ErrorObservable" ))
		{
			ErrorObservable::$_instance = new ErrorObservable;
		}
		return ErrorObservable::$_instance;
	}
}

/**
 *	IError interface
 *	Sets API for Observers
 */
interface IError
{
	public function notify( $message );
}

/**
 *	A concrete class implementing the observer interface
 *	Adds the ability to react to the given state
 */
class ObserverClass implements IError
{
	/**
	 *	Defines the thing to watch for
	 *	@var mixed $_watch_for
	 *	@access private
	 */
	private $_watch_for = _ENTRY_POINT_FAILURE;
	/**
	 *	Constructor
	 *	Adds object to Observer array
	 */
	public function __construct()
	{
		ErrorObservable::getInstance()->addObserver( $this );
	}
	/**
	 *	Implemented notify function
	 *	Gets called by the Observable
	 *	@access public
	 *	@param mixed $message
	 *	@return boolean
	 */
	public function notify( $message )
	{
		if( $message == $this->_watch_for )
		{
			die( $message );
		}
		return false;
	}
}

/**
 *	Some example code
 *	Makes an observer object instance and triggers the observable
 */
class EntryPointAction
{
	function __construct()
	{
		$oc = new ObserverClass;// Make an observer class
		switch( isset( $isEverythingOK ))
		{
			case 1:
				ErrorObservable::getInstance()->setState( _ENTRY_POINT_OK ); // Set a state in observable object
				break;
			case 0:
				ErrorObservable::getInstance()->setState( _ENTRY_POINT_FAILURE ); // Set a state in observable object
				break;
		}
		ErrorObservable::getInstance()->notify(); // Notify all observers
	}
}

$epa = new EntryPointAction;
    Write a Reply...