Location: PHPKode > scripts > Event Handler > event-handler/class.Event.php
<?php
/**
* @package Event class
* @author Jean Machuca
* @version 1.0.20060729
* @example 
* 	// main example class
* 	class main (){
* 		var $onLoad; // example Load event
* 		var $oEvent; // event handler
* 		function main(){
* 			$this->onLoad = array(&$this,'Load'); // simple event assignment
* 			$oEvent = new Event($this); // constructs the event handler
* 			$this->oEvent = &$oEvent; // assign event handler to object by reference
* 			$this->oEvent->fire('Load'); // fire the Load event
* 			return $this;
* 		}
* 		
* 		// Load event handler
* 		function Load(){
* 			echo "Load event is fired.";
* 			return true;
* 		}
* 	}
*
*/
class Event {
	var $aEvents = array();
	var $oClass;
	var $eventPrefix = 'on';
	var $functionPrefix = 'fn';
	
	/**
	 * Event Constructor
	 *
	 * @param object &$class Instance of object that contains events
	 * @param string [$eventPrefix] Nomenclature Prefix for event property name
	 * @param string [$functionPrefix] Nomenclature Prefix for event function name
	 * @return Event
	 */
	function Event(&$class,$eventPrefix = 'on',$functionPrefix = 'fn'){
		$this->eventPrefix = $eventPrefix;
		$this->functionPrefix = $functionPrefix;
		$this->oClass = &$class;
		if ($this->setDefaultEvents($this->oClass) && $this->catchAll($this->oClass)){
			return $this;
		} else {
			return null;
		}
	}
	
	/**
	* Fires the indicated event
	* 
	* @param string $eventName Name of the event
	* @param array $eventParams array Event params. 
	*/
	function fire($eventName,$eventParams = array()){
		if (is_object($this) && is_callable($this->aEvents[$eventName])){
			return call_user_func_array($this->aEvents[$eventName],$eventParams);
		} else{
			return false;
		}
	}
	
	/**
	* Catchs a user call for an indicated event
	* 
	* @param string $eventName Name of the event
	* @param string,array $userMethod string or array of the method call
	*/
	function catch($eventName,&$userMethod){
		if (is_object($this) && strtolower(gettype($eventName)) == 'string' && $eventName != ""){
			$this->aEvents[$eventName] = &$userMethod;
			return true;
		} else {
			return false;
		}
	}
	
	/**
	* 
	* Automatic catchs the object events in extended object of event class or return the event object with the catched events
	* @param object $oClass instance of a class
	* @param string $eventPrefix prefix of the class var name with contains the event
	* @return object instance of event class with the catched events
	*/
	function catchAll(&$oClass){
		if (is_object($this)){
			$this->oClass = &$oClass;
			$aClassVars = get_class_vars(get_class($this->oClass));
			$aClassVarNames = array_keys($aClassVars);
			foreach ($aClassVarNames as $varName){
				if (!is_numeric($varName) && (substr($varName,0,strlen($this->eventPrefix)) == $this->eventPrefix)){
					$eventName = substr($varName,strlen($this->eventPrefix));
					$this->catch($eventName,$this->oClass->$varName);
				}
			}
			return $this->oClass;
		} else {
			return false;
		}
	}

	/**
	* 
	* Automatic set the prefixEvent type events to Event method of the given reference class instance
	* @param object $oClass instance of a reference class
	* @param string $eventPrefix prefix of the class var name with contains the event
	* @return object instance of event class with the catched events
	*/
	function setDefaultEvents(&$oClass){
		if (is_object($this)){
			$this->oClass = &$oClass;
			$aClassVars = get_class_vars(get_class($this->oClass));
			$aClassVarNames = array_keys($aClassVars);
			foreach ($aClassVarNames as $varName){
				if (!is_numeric($varName) && (substr($varName,0,strlen($this->eventPrefix)) == $this->eventPrefix)){
					$eventName = substr($varName,strlen($this->eventPrefix));
					if ($oClass->$varName == null) $oClass->$varName = array(&$oClass,$this->functionPrefix.$eventName);
				}
			}
			return $this->oClass;
		} else {
			return false;
		}
	}
	
	function __destruct(){
		unset($this);
	}
}
?>
Return current item: Event Handler