Event driven programming in PHP - Event Class




Download the demo code

Lets have a look at what our code looks like for managing events.

Notice the following methods:

Subscribe (assign function(s) that must be executed as soon as the event is raised)
Unsubscribe (remove function(s) from the event)
Raise (execute function(s) subscribed to the event)
Subscriptions (returns how many handlers are attached to the event)

 
class Event
{
	private $callbacks;
 
	public function Event()
	{
		$this->callbacks = array();
	}
 
	private function Get_Callback($args)
	{
		return (count($args) < 2) ? new Callback($args[0], '') : new Callback($args[1], get_class($args[0]));
	}
 
	public function Subscribe()
	{		
		$callback = $this->Get_Callback(func_get_args());
		if (!in_array($callback, $this->callbacks))
		{		
			$this->callbacks[] = $callback;
		}
		else throw new Exception($callback->method.' already subscribed to this event');
	}
 
	public function Unsubscribe()
	{
		$callback = $this->Get_Callback(func_get_args());
		$key = array_search($callback, $this->callbacks);		
		if (!($key === false))
		{
			unset($this->callbacks[$key]);
		}
		else throw new Exception($callback->method.' not subscribed to this event');		
	}
 
	public function Raise()
	{
		foreach($this->callbacks as $callback)
		{
			if (method_exists($callback->context, $callback->method) || function_exists($callback->method))
			{				
				$params = func_get_args();
				$callback = (!empty($callback->context)) ? array($callback->context, $callback->method) : $callback->method;
				call_user_func_array($callback, $params);
			}
			else throw new Exception($callback->method." does not exist");
		}
	}
}
 


The following class contains the name and context(current scope) where the function can be found. Context can either be the current class onload->Subscribe($this, "somemethod") or the global application onload->Subscribe("somefunction").

Callback Class

 
class Callback
{
    public $method;
    public $context;
 
    public function Callback($method, $context)
    {
        $this->method = $method;
        $this->context = $context;
    }
}
 








No Entries Found

Post comment

Name *
Email
Title
Body *
Security code
*
* Required fields

Latest Articles

C# : Snippets


Collection of C# snippets
2010-05-22 01:06:19

MS SQL : Snippets


Collection of MS SQL snippets
2010-05-22 00:55:15

JavaScript : Snippets


Collection of JavaScript snippets
2010-05-22 00:37:57

ASP.net: Snippets


Collection of ASP.net snippets
2010-05-22 00:29:56

PHP: Snippets


Collection of PHP snippets
2010-05-22 00:06:45

Parallel Language Reference : Strings


a Parallel reference of programming languages
2009-09-10 12:48:23

PHP Tutorial: Developing a Login – Part 1


a tutorial explaining how to develop a simple login using PHP and MySQL
2009-09-05 18:26:47

Event driven programming in PHP


An article looking at adding some kind of event driven model to PHP 5
2008-07-28 12:48:09

How to create your own RSS Reader


It is very simple creating your own rss reader, the following article looks at a few methods of doing this.
2008-06-23 13:18:25

Javascript Reference: Dropdown


A quick reference about working with dropdown boxes (select element) in javascript.
2007-02-17 16:36:41

Top 5 Articles

Programming humor


Collection of funny programming articles
2006-10-08 14:23:43

How to create your own RSS Reader


It is very simple creating your own rss reader, the following article looks at a few methods of doing this.
2008-06-23 13:18:25

Javascript Reference: Dropdown


A quick reference about working with dropdown boxes (select element) in javascript.
2007-02-17 16:36:41

PHP: Snippets


Collection of PHP snippets
2010-05-22 00:06:45

Event driven programming in PHP


An article looking at adding some kind of event driven model to PHP 5
2008-07-28 12:48:09