PHP Events - Actionscript 3 style events in PHP 5

Here are a set of classes which aim to provide basic actionscript style events in php5;

The Point - Events are useful everywhere even when you don't have a UI, the main reason for building this was to create a kind of "hook" system for php. Often we create multi site systems where each implementation of that system has different client specific considerations; consider a system where one client wants all of there images to go to amazon s3 instead of being saved on the host like your other clients. With this system you can simply:

  1. throw an "storeUpload" event which is cancelable
  2. register a default event listener which listens for "storeUpload" events and provides default functionality, saving the upload and setting the location (ur) on the event.target
  3. on one site register a custom event listener which listens for the "storeUpload" event, uploads to amazon s3, then cancels the event and set the event.target to the s3 url instead

as you can see, with this you can keep the codebase of your multisite application the same and simply hook in to events whereever you want.

the system consists of 2 public classes:

Event

The base class for all events, provides event type and cancelable; with typical usage you create a new event, then dispatch it when ready.

  • __construct( $type , $cancelable=FALSE )
    create a new event listener - if you extend the Event class you must call this with parent::__construct( $type , $cancelable );

    $type - string, event type eg "debugEvent"
    $cancelable - boolean, provides cancelation functionality, so you can prevent further event listeners from firing thus in effect overriding them.

    throws InvalidArgumentException if $type is not a string

  • cancel() : void
    cancels an event, only if it is cancelable
  • isCancelable() : Boolean
  • isCanceled() : Boolean
  • getType() : string
  • getTarget() : obj
    target always contains the instance which dispatched the event.

EventDispatcher

All classes which wish to dispatch events must inherit from EventDispatcher (like in most languages), this class provides:

  • addEventListener( $type , $callable , $owner ) : void
    adds an event listener, this may be a function, a class method or a static class method.
    note: a current limitation is that event listener methods must be public

    $type - string, event type eg "debugEvent"
    $callable - string, the function or method which is to be called when event $type is dispatched
    $owner - mixed, used when $callable is a method, can be either an object instance, or the string name of a class for static methods.

    throws:
    InvalidArgumentException when type or callable isn't a string
    Exception('unknown method / function')

  • removeEventListener( $type , $callable , $owner ) : void
    removes an event listener that has been added with addEventListener, this only removes a single event listener each call, so if you have 3 identical event listeners you will need to call this method 3 times to remove them all.

    arguments are the same as addEventListener - throws no exceptions, always returns void

  • hasEventListener( $type ) : Boolean
    checks if any event listeners are registered for event $type; always returns boolean true or false.
  • dispatchEvent( Event $event ) : void
    the obvious one, dispatches an event and notifies all listeners.

Notes:

The event dispatcher implements a function which hides all instance properties starting with __ from the serialize process, thus hiding the events and meaning your current serialization process will still work, additionally you can utilize it to hide any properties you don't want to be serialized.

I've tried to make the classes as simple, small and practical as possible, they execute rather quickly and shouldn't make any speed difference to your application - do however be aware that loosely coupling and entire system could allow coding errors to creep in, so maybe don't use it everywhere - but then PHP isn't strict anyways so..!

Download

You can download the PHP Event system here (3 classes + example, 4.2k) - hope somebody finds it useful!

events

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Printed from: http://webr3.org/blog/experiments/php-events-actionscript-3-style-events-in-php-5/ .
© Your Name Here 2010.

7 Comments   »

  • tonypee says:

    Nice.
    If you re-wrote this in haxe/php (www.haxe.org) then you would be able to have as3 like syntax, tpying, completion AND events. Pretty much as3 serverside - with all of the benefits of php deployment ;P

  • tonypee says:

    hah - how did i end up back here?!

  • nathan says:

    lol tony - what ya doing :p good idea on the haxe port, may do it - but saying that I hate targetting php with haxe, because i moved to haxe from php for very good reasons, so would rather write it then target neko or something (or flash/as3 lmao - lol - hahahahaha -lol ahhh dear)

  • Absulit says:

    This is really nice, I'll gonna try it out, and I'll come back later to tell you my experience

  • Absulit says:

    Hi I'm back!
    Hard to understand at the beginning, but at the end this is really simple and neat;
    the code looks great and without errors!
    I created a Class to connect to a MySQL DB, so it extends the EventDispatcher Class and dispatches my customized events (onError, onComplete, and onCompleteQuery) and it is exactly what I need ;)
    thanks a lot
    I suscribed to your feed to ;)

  • Matthew Bonner says:

    I like the idea but I would have liked something more along the lines of a static dispatcher, so something like:
    new Event(System_Event::ready);

    Which would then run
    Framework::initialise();

    If the following has been executed previous to calling the new event
    Event_Observer::addAction(array('Framework', 'initialise'), System_Event::ready);

  • nathan says:

    Quite sure you could modify the code to make a static dispatcher; should only be one class with a couple of very simple methods.

    you can already call methods statically, and of course you can make your Events any way you want.

    maybe you just use these classes to do the grunt work and build a light wrapper around it :)

RSS feed for comments on this post , TrackBack URI

Leave a Reply

Additional comments powered by BackType

  • webr3 avatar