Home arrow PHP arrow Page 2 - Simulating Events with PHP 5

The EventHandler and EventHandlerCollection Classes - PHP

PHP has the drawback of not supporting events. Fortunately, a basic structure can be built to support events in PHP 5. This article tackles that problem with some proof of concept code.

  1. Simulating Events with PHP 5
  2. The EventHandler and EventHandlerCollection Classes
  3. The Event-Enabled Class
  4. Using the Event-Enabled Class
By: David Fells
Rating: starstarstarstarstar / 31
February 20, 2006

print this article



To create an event handler, we need to know the event to which it was attached and we need to know what to do when the handler finds out about the event being raised. Since we cannot create function references, and lack of native support for events eliminates the usefulness of anonymous functions, we will use an approach similar to C# delegates, but with less fuss. We will create an EventHandler object by passing it an Event object and the name of the callback function, which will later be called in an eval() statement (ugly, but it will do).

class EventHandler
     private $event;
     private $callback;    

     public function GetEventName()
          return $this->event->GetName();

     public function __construct($event, $callback)
          $this->event = $event;
          $this->callback = $this->PrepareCallback($callback);

     public function Raise($sender, $args)
          if ($this->callback)

     private function PrepareCallback($callback)
          if ($pos = strpos($callback, '('))
              $callback = substr($callback, 0, $pos);    

          $callback .= '($sender, $args);';         

          return $callback;

This class actually has some substance to it. The Raise() method actually runs the callback, and the PrepareCallback() method makes sure the callback has the proper signature for a delegate, that is, (Object $sender, EventArgs $e), for those of you who are familiar with C#. Since I didn’t care to make an EventArgs class, $args will just be an array. This provides the handler functions with context that they would otherwise lack. They know the calling object, $sender, and they can be given parameters with $args.

The EventHandlerCollection also provides a test for membership with the Contains() method, basically what we saw with the EventCollection, and it provides a RaiseEvent() method that picks through its list of handlers and calls Raise() on the right one. This keeps event triggering simple later on when we look at the event enabled class. Here is the EventHandlerCollection class:

class EventHandlerCollection
     private $handlers;    

     public function __construct()
          $this->handlers = new ArrayObject();

     public function Add($handler)

     public function RaiseEvent($event, $sender, $args)
          foreach ($this->handlers as $handler)
              if ($handler->GetEventName() == $event)
                   $handler->Raise($sender, $args);

We now have our four “framework” classes. It is time to move on to a class that can actually register event handlers and raise events.

>>> More PHP Articles          >>> More By David Fells

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: