Writing an Event System in PHP
In this tutorial you will learn how to create and implement an event system in PHP. You will be able to integrate it with your own web application or framework.
What is an event system?
An event system is a way to call prefined functions at a specified time in your application code. It’s main use is to allow developers to add functionality to your code without modifying the original source. Directly editing the source code of an application is proven not to work because every time the software gets updated, the changes made to the source get overwritten. By offering an event system in your application developers will be able to write plugin style code which will get executed at a specified time in the main application code. This makes your platform easier to develop for and offers a low entry level for coders wanting to add functionality themselves.
Overview of the system
event::$events is a static variable (array) which holds all of the registered events and associated Closure objects.
event::register(‘eventName’, function($args){}) accepts two arguments. The first being the name of the event and the second being a Closure object which will be called when the event is fired. You can see we use type hinting for the second argument to only accept a Closure object, this way we don’t need to write extra code in the function to check whether the method is callable.
public static function register($event, Closure $func)
{
self::$events[$event][] = $func;
}
event::fire(‘eventName’, $args) also accepts two arguments. The first is the event name and the second is an array of arguments that will be passed to the Closure function when it is called. Because we can register multiple methods to one event, we need to iterate over the array of events and then call the function with passed arguments.
public static function fire($event, $args = array())
{
if(isset(self::$events[$event]))
{
foreach(self::$events[$event] as $func)
{
call_user_func($func, $args);
}
}
}
Usage
Suppose we were writing a blogging platform and we wanted to make it easy for developers to call functions when a new blog post was created or deleted. In the appropriate location we would call the fire event::method(). In this case we are passing $postInfo , an array of post data, as an argument
event::fire('blog.post.create', $postInfo);
If we wanted to send an email to someone every time a post is published we only need to register an event and include the code.
event::register('blog.post.create', function($args = array()){
mail('myself@me.com', 'Blog Post Published', $args['name'] . ' has been published');
});
Alternatively, if the event system were put to use in a framework, there could be events for various times during the framework’s loading process (eg. before routing, before calling the controller function, before accessing the database). We would call the fire function at these times.
event::fire('pre.controller');
event::register('pre.controller', function($args = array()){
log::write('pre controller');
//include some relevant files
});
Full source
class event
{
public static $events = array();
public static function fire($event, $args = array())
{
if(isset(self::$events[$event]))
{
foreach(self::$events[$event] as $func)
{
call_user_func($func, $args);
}
}
}
public static function register($event, Closure $func)
{
self::$events[$event][] = $func;
}
}
If you have any questions or comments about the event class please ask below in the comments.



July 1, 2010
I have been looking for this for a long time. Best tutorial about event systems!!! Thanks alot!
October 20, 2010
Great tutorial. A question though. How would I go about adding functionality to parse a variable before it is displayed, much like the add_filter() function in WordPress?
Help would be much appreciated!