HTML5 Zone is brought to you in partnership with:

Evert Pot is a web architect, blogger and open-source developer. Evert specialized in the HTTP protocol and the LAMP stack. His interests lie in making systems and development teams scale. Evert is a DZone MVB and is not an employee of DZone and has posted 12 posts at DZone. You can read more from them at their website. View Full User Profile

sabre-event, a simple event management library for PHP 5.4

07.22.2013
| 2140 views |
  • submit to reddit

I just released version 1.0 of sabre-event, a simple event management library for PHP, heavily inspired by both nodejs' EventEmitter, and Igor Wielder's Événement.

This library has a few extra features not in Événement that I really needed, and Igor had no plans adding it to his library, so I wrote my own.

In a nutshell, this is how you use it:

<?php

use Sabre\Event\EventEmitter;

include 'vendor/autoload.php';

$eventEmitter = new EventEmitter();

// subscribing
$eventEmitter->on('create', function() {

    echo "Something got created, apparently\n"

});

$eventEmitter->emit('create');

?>

The EventEmitter object can also be integrated into existing objects, by extending it, or using it as a trait:

<?php

use Sabre\Event;

class MyNotUneventfulApplication implements Event\EventEmitterInterface
{

    use Event\EventEmitterTrait;

}

?>

It differs from Événement in two features:

  1. It's possible to prioritize listeners, and let them trigger earlier or later in the event chain.
  2. It's possible for listeners to break the event chain, much like javascript's preventDefault().

I hope it will be useful to others. You can find the full documentation on GitHub, and the preferred installation method is through Composer.

Published at DZone with permission of Evert Pot, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)