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

Using the Event-Enabled Class - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Using MyEventClass is fairly straightforward. You create an EventHandlerCollection and any event handling methods you want, and you instantiate the class.

$handlers = new EventHandlerCollection();
$handlers->Add(new EventHandler(new Event('OnLoad'),
'handleLoad'));
$handlers->Add(new EventHandler(new Event('OnUnload'),
'handleUnload'));

$obj = new MyEventClass($handlers);
$obj = null;

function handleLoad($sender, $args)
{
     print 'object '.get_class($sender).' loaded with '.count
($args).' args!<br />';
}

function handleUnload($sender, $args)
{
     print 'object '.get_class($sender).' unloaded with '.count
($args).' args!';
}

If you create a PHP page with all the code in it from this article and run it, you will see the following on your screen:

object MyEventClass loaded with 1 args!
object MyEventClass unloaded with 1 args!

Obviously this is a rather minimalistic (and pointless) implementation, but it does demonstrate the usage of the event handlers and the event-enabled class. Pretty cool, yes?

Conclusion

Hopefully this article has piqued your interest and you will experiment, improve my code, and create a fantastic open source event framework for PHP, which I am far, far too lazy to do myself. There are some obvious points where refactoring is called for, such as creating an interface or an abstract base class that event-enabled classes adhere to, or providing a more effective design of the event and handler classes. I intend to use the code Iíve created here in my next PHP project, and I hope you will, too. Be sure to let me know how it works out and post any nifty improvements in the dicussion thread of the article.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: