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

4

7

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.

Pathos answered 2/10, 2011 at 20:16 Comment(0)
E
4

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

Exterminatory answered 10/10, 2011 at 23:38 Comment(2)
lame, but the only option for now :)Pathos
@Pathos why not start another PSR comparing Symfony2, ZF2, Aura, Fuel... and who else... Anyway not sure it will get accepted or not. May be a base interface only applies.Watering
G
2

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.

Geometrician answered 2/10, 2011 at 21:33 Comment(0)
P
2

You need to implement observer pattern by implementing PHP interface SplObserver , SplSubject. Not just Zend , Symphony does that to support hooks but generally every event dispatcher work this way by implementing observer pattern .

Here is an article to know more http://devzone.zend.com/article/4284

Paez answered 3/10, 2011 at 1:48 Comment(1)
Hi OSL, take a look at the edit, the point of the question is not which pattern to use, it's obvious that you nedd to go with the observer ;-)Pathos
F
1

Old post that has already been accepted but there is a solution for a drop in EDP solution in PHP for those that come across this as I have.

http://prggmr.org

The functionality is much different than that of Symfony's and Zend's implementation as their is no interface or classes that need extending to use the library, rather you simply call typical php functions to handle to the event dispatching.

// Subscribe to dispatched events
subscribe(callback, signal)

// Dispatch an event
fire(signal)
Farsighted answered 6/3, 2012 at 19:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.