Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to integrate an event-dispatcher in a PHP library?

I need to integrate an event dispatcher in my own codebase (custom PHP library), so I looked at what both Symfony2 and Zend Framework 2 are doing.

Obviously, there's no shared interface for dispatching events, because both frameworks have different needs and decided to implement their own code... so I am a bit lost: I don't want to reinvent my personal wheel.

Probably the SPL interfaces for implementing the observer pattern are a bit naive, so I'm here asking you: what would you do?

EDIT

Since it's not clear... I want to re-use an existing ED, letting the developer inject it in my library.

Let's say you develop a lib with a dispatcher and you know that your lib is gonna be a part of a Symfony Bundle and also re-used in ZF projects: you surely want to re-use Symfony's and ZF dispatchers, instead of your own.

Therefore I was looking for shared interfaces for existing dispatchers implemented in mainstream libraries, but sounds like there's no solution.

like image 319
odino Avatar asked Oct 02 '11 20:10

odino


2 Answers

You could define an interface for your needs and then implements it with differents adapters for each framework.

like image 145
Francesco Terenzani Avatar answered Oct 17 '22 13:10

Francesco Terenzani


I think your first instinct to pick one of the widely used components is the way to go.

Those two are the options I would be considering as well. You should simply take a look at both of them and pick the one you feel will work best for you.

Shameless plug: If you want something really, really lightweight, you can take a look at Événement.

like image 41
igorw Avatar answered Oct 17 '22 12:10

igorw