PHP
  Home arrow PHP arrow 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? 
Google.com  
PHP

Simulating Events with PHP 5
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 23
    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
    ( Page 1 of 4 )

    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.

    One of the big drawbacks of PHP has always been its lack of events. Events allow programmers to attach new behaviors to objects that are activated when certain conditions are met. Those conditions are announced to the outside world as an event. While all object languages that support events do so a bit differently, some being very simple like JavaScript or VB.NET, others being a real pain in the rear like C#, it should go without saying that the task of creating a framework to simulate events will be much harder.

    It seemed reasonable to me that some sort of basic structure could be established to support events in PHP 5, so I set out to whip something up as quickly as possible as a proof of concept. The contents of this article are the work of roughly one programming hour and surely stand to be improved upon, but the basic idea is this: instantiate an object and attach event handlers; the handlers will be executed when the events they are associated with are raised.

    This is not an article for beginners, but I would not say you have to be a guru to understand the concepts and the code here. I have tried to keep it as minimal as possible to stay on point.

    The Event and EventCollection Classes

    First we need to create a simple object to store whatever event information we may need and an object to contain a collection of events. The reason we do not simply store them in an ArrayObject is because we need to be able to quickly check to see whether an event exists in a given collection, which requires code written specifically for the task.

    Here is the Event class:

    class Event
    {
         private $name;    

         public function GetName()
         {
              return $this->name;
         }    

         public function __construct($name)
         {
              $this->name = $name;
         }
    }

    As you can see, the class is essentially a container for a string. You could do away with this class in the examples given here, but I chose to go ahead and use an Event class in case at some point we need to store more information about a particular event. The EventCollection class basically wraps an ArrayObject and provides a function to check for an event by name.

    class EventCollection
    {
         private $events;

         public function __construct()
         {
              $this->events = new ArrayObject();
         }

         public function Add($event)
         {
              if (!$this->Contains($event))
                  $this->events->Append($event);
         }    

         public function Contains($event)
         {
              foreach ($this->events as $e)
              {
                  if ($e->GetName() == $event)
                       return true;
              }
         }
    }

    Again, I chose to use classes for events rather than simply an ArrayObject of strings to provide support for future additions. There is no code in either of these classes that requires explanation, so let us continue.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek