All pastes #92904 Raw Edit

Ray

public php v1 · immutable
#92904 ·published 2006-07-20 00:46 UTC
rendered paste body
<?phperror_reporting(E_ALL);/*interface IPlugin {	function getEvents();	function React($Event);	function eventExists($Event);}interface IPluggable {	function fireEvent($Event);	function getHandlers($Event);	function listPlugins();	function eventExists($Event);	function Register($Plugin,$Event);}*/class Plugin {	protected $Events;	protected function eventExists($Event) {		return in_array($Event, $this->Events);	}	public function React($Event,$Object) {		if($this->eventExists($Event)) {			$this->Events[$Event]($Object);		}	}	public function getEvents() {		return $this->Events;	}	}class PluggableObject {	protected $Events;	protected function fireEvent($Event) {		//echo "Firing event \"$Event\"\n";		if($this->eventExists($Event)) {				foreach($this->getHandlers($Event) as $Handler) {		//		echo "Plugin found! Plugin: $Handler\n";				$Plugin = new $Handler;				$Plugin->React($Event,$this);			}					} else {			echo "Event does not exist! (\"$Event\")\n";			die("Events that DO exist: ".print_r($this->Events,1));		}	}	protected function eventExists($Event) {		return in_array($Event, $this->Events);	}	protected function listPlugins() {		return join(", ", $this->Plugins).".";	}	protected function getHandlers($Event) {		return $this->Events[$Event];	}	public function Register($Plugin, $Event) {		if($this->eventExists($Event)) {			$this->Events[$Event][] = $Plugin;		}	}	}class Athlete extends PluggableObject/* implements IPluggable*/ {	private $Energy = 100;	private $Miles;	private $Name;	const MilesEnergyRatio = 10.5;	const Tired = "I'm tired!";	private $Plugins = array();	protected $Events = array(Athlete::Tired);		public function Run($Miles) {		if(is_numeric($Miles) && $Miles > 0) {			$Miles = round($Miles,2);			if($this->Energy > ($Miles * Athlete::MilesEnergyRatio)) {				$this->Miles += $Miles;				$this->Energy -= $Miles * Athlete::MilesEnergyRatio;				echo "I'm running $Miles miles!\n";				if($this->Energy <= 1) {echo Athlete::Tired."\n"; $this->fireEvent(Athlete::Tired);}			} else {				$Runable = $this->Energy / Athlete::MilesEnergyRatio;				$Runable = round($Runable,2);				echo "I could not run $Miles miles, so I'm running $Runable miles!\n";				$this->Run($Runable); 											}			//			print "DEBUG:\n\tEnergy: ".$this->Energy."\n\tFull: ".$this->Full."%\n";		}	}	public function Set($Variable, $Value) {		$this->{$Variable} = $Value;	}	public function Get($Variable) {		return $this->{$Variable};	}	public function __construct($Name) {		$this->Name = $Name;	}		}class Rest extends Plugin /*implements IPlugin */{	protected $Events = array(Athlete::Tired);	public function __construct() {		$this->Events[Athlete::Tired] = create_function('$Athlete',					'if($Athlete instanceof Athlete) {						echo "I\'m resting...\n";						$Athlete->Set("Energy",100);					 }');//	echo "I'm being created!\n";	}}$John = new Athlete("John");$John->Register("Rest",Athlete::Tired);$John->Run(3);$John->Run(4);print $John->Get("Name")."'s energy: ".$John->Get("Energy")."\n";$John->Run(3);print $John->Get("Name")."'s energy: ".$John->Get("Energy")."\n";$John->Run(2);print $John->Get("Name")."'s energy: ".$John->Get("Energy")."\n";?>