开发者

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

开发者 https://www.devze.com 2023-04-09 20:07 出处:网络
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.

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 Sy开发者_如何转开发mfony'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.


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


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.


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


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号