PHP
  Home arrow PHP arrow Page 2 - Simulating Events with PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Simulating Events with PHP 5
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 20
    2006-02-20


    Table of Contents:
  • Simulating Events with PHP 5
  • The EventHandler and EventHandlerCollection Classes
  • The Event-Enabled Class
  • Using the Event-Enabled Class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Simulating Events with PHP 5 - The EventHandler and EventHandlerCollection Classes
    ( Page 2 of 4 )

    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)
                  eval($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)
         {
              $this->handlers->Append($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
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT