将事件调度程序集成到 PHP 库中的最佳方法是什么?

发布于 2024-12-07 16:29:52 字数 694 浏览 0 评论 0原文

我需要在我自己的代码库(自定义 PHP 库)中集成一个事件调度程序,所以我查看了 Symfony2Zend Framework 2正在做。

显然,没有用于调度事件的共享接口,因为两个框架都有不同的需求并决定实现自己的代码......所以我有点迷失:我不想重新发明我的个人轮子。

可能用于实现观察者模式的 SPL 接口有点幼稚,所以我在这里问你:你会做什么?

编辑

因为还不清楚...我想重用现有的ED,让开发人员将其注入我的库中。

假设您使用调度程序开发一个库,并且您知道您的库将成为 Symfony Bundle 的一部分,并且也会在 ZF 项目中重复使用:您肯定希望重用 Symfony 和 ZF 调度程序,而不是您自己的调度程序。

因此,我一直在寻找主流库中实现的现有调度程序的共享接口,但听起来没有解决方案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

望笑 2024-12-14 16:29:52

您可以根据需要定义一个接口,然后为每个框架使用不同的适配器来实现它。

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

夜夜流光相皎洁 2024-12-14 16:29:52

我认为你的第一直觉是选择一个广泛使用的组件是正确的选择。

这两个也是我会考虑的选择。您应该简单地看一下它们,然后选择您认为最适合您的一个。

无耻插件:如果你想要真正非常轻量级的东西,你可以看看 Événement

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.

终难遇 2024-12-14 16:29:52

您需要通过实现 PHP 接口 SplObserver 、 SplSubject 来实现观察者模式。不仅是 Zend,Symphony 也这样做是为了支持钩子,但通常每个事件调度程序都通过实现观察者模式来以这种方式工作。

这是一篇文章,了解更多信息 http://devzone.zend.com/article/4284

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

递刀给你 2024-12-14 16:29:52

旧帖子已被接受,但对于那些像我一样遇到此问题的人来说,有一个解决方案可以在 PHP 中删除 EDP 解决方案。

http://prggmr.org

该功能与 Symfony 和 Zend 的实现有很大不同,因为它们没有接口或类需要扩展才能使用该库,而不是简单地调用典型的 php 函数来处理事件调度。

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

// Dispatch an event
fire(signal)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文