PHP
  Home arrow PHP arrow Page 3 - 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 / 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 - The Event-Enabled Class
    ( Page 3 of 4 )

    To enable a class with events, there are a few prerequisites, given our existing classes. First, it needs to contain a list of available events. Second, it needs to be able to trigger, or raise, its events through some helper method. Third, it needs to be able to register event handlers. Note that any given event can have more than one event handler.

    The code for this class, which we are calling MyEventClass, is actually pretty simple. In the code below, you will notice that the class is wired up for two events, OnLoad and OnUnload. You will also notice that we pass an EventHandlerCollection into the constructor. To support the OnLoad event, which fires at the end of the constructor, we would have to already have a handler in place to make use of it – which means, in general, classes implementing this pattern will need to provide a $handlers parameter in the constructor.

    class MyEventClass
    {
         private $events;
         private $eventHandlers;    

         public function __construct(EventHandlerCollection $handlers
    = null)
         {
              $this->events = new EventCollection();         

              if ($handlers)
                  $this->eventHandlers = $handlers;
              else
                  $this->eventHandlers = new EventHandlerCollection
    ();          

              $this->InitEvents();
              $this->TriggerEvent('OnLoad', array('arg1'=>1));
         }    

         public function __destruct()
         {
              $this->TriggerEvent('OnUnload', array('arg2'=>2));
         }    

         public function RegisterEventHandler($handler)
         {
              if ($this->events->Contains($handler->GetEventName()))
                  $this->eventHandlers->Add($handler);
         }
         private function InitEvents()
         {
              $this->events->Add(new Event('OnLoad'));
              $this->events->Add(new Event('OnUnload')); 
         }
         private function TriggerEvent($eventName, $args)
         {
              $this->eventHandlers->RaiseEvent($eventName, $this,
    $args);
         }
    }

    To quickly review the class methods, from the bottom up, there is TriggerEvent(), which accepts an event name and any relevant arguments. It directly calls EventHandlerCollection::RaiseEvent, which passes the event along to the appropriate handler or handlers, if one does in fact exist. Since the RaiseEvent() method of EventHandlerCollection does not have an exit condition when it finds a handler matching the raised event, we can have as many handlers for an event as we want.

    InitEvents() is a crude way to register available events with the class. We have to populate the EventCollection somehow, and this seemed to me the best way to do it. RegisterEventHandler() is simply a wrapper for EventHandlerCollection::Add(), allowing us to attach handlers to an object after it has been instantiated. I would suggest passing all handlers in the constructor for clarity's sake, but the RegisterEventHandler() method is available nonetheless.

    Notice in __construct() and __destruct(), TriggerEvent() is called. The result of calling TriggerEvent(), through a few levels of indirection, calls the code you passed in when you created your EventHandlers. That brings me to the final bit of code for this article – using MyEventClass.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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