PHP Event-Listener最佳实践实现 [英] PHP Event-Listener best-practice implementation

查看:100
本文介绍了PHP Event-Listener最佳实践实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用PHP创建类似于CMS的系统.使其尽可能模块化和可扩展.

I am trying to create a CMS-like system in PHP. making it as modular and extendable as possible.

有人能为我提供在PHP中创建事件侦听器系统(例如Drupal系统的非常简化版本),创建钩子并在一个简短的示例中实现它们的最佳实践方案.

Could someone offer me the best-practice scenario of creating a event-listener system in PHP (a very simplified version of Drupal system for example), creating hooks and implementing them in a short example would also be nice.

推荐答案

从实现的角度来看,确实有三种不同的实现方法(请注意,这些是OO设计模式,但是您可以在功能上或过程上实现它们想).

Well, there's really three different ways of doing this from an implementation perspective (note that these are OO design patterns, but you could implement them functionally or procedurally if you wanted to).

1.观察者模式

您可以实现观察者模式.基本上,您会将可以引发事件的所有事情作为主题.然后,您要收听的类/代码将绑定到它想收听的内容.因此,假设您有一个名为Foo的控制器.如果您想听,可以拨打$fooController->attach($observer);.然后,无论何时控制器想要说些什么,它都会将事件分派给所有观察者.

You can implement the Observer Pattern. Basically, you'd have each thing that can raise events be a subject. Then the classes/code you want to listen binds to what it wants to listen to specifically. So let's say you have a controller called Foo. If you wanted to listen to it, you could call $fooController->attach($observer);. Then, whenever the controller wanted to say something, it would dispatch the event to all of the observers.

这确实非常适合于通知系统(扩展类正在执行的操作).它不太适合实时修改代码的行为.

This is really well suited for a notification system (to extend what classes are doing). It's not as well suited for modifying the behavior of code in real time.

2.装饰图案 您还可以实现装饰器模式.基本上,您要获取要修改的对象,然后将其包装"到一个您要更改的新对象中.这确实非常适合修改和扩展行为(因为您可以从包装的类中选择性地覆盖功能).

2. Decorator Pattern You can also implement the Decorator Pattern. Basically, you take the object that you want to modify, and "wrap" it in a new object that does what you want to change. This is really well suited for modifying and extending the behavior (since you can selectively override functionality from the wrapped class).

如果您已定义接口并期望对象符合它们,则此方法非常有效.如果您没有接口(或没有正确使用它们),那么装饰器模式可以为您做的大部分事情都会丢失.

This works very well if you have defined interfaces and expect objects to conform to them. If you don't have interfaces (or don't use them properly), most of what the decorator pattern can do for you will be lost.

还请注意,这实际上不是事件的一种处理方式,而是一种修改对象行为的方式.

Also note that this really isn't a way of doing events, it's a way of modifying object behavior.

3.中介者模式

您还可以使用 Mediator .基本上,您将拥有一个全球调解员来跟踪您的听众.当您要触发事件时,可以将事件发送给调解器.然后,中介者可以跟踪哪些侦听对象想要接收该事件,并正确传递消息.

You could also use a Mediator. Basically, you'd have one global mediator that keeps track of your listeners. When you want to trigger an event, you send the event to the mediator. The mediator can then keep track of which listening objects want to receive that event, and pass the message along properly.

这具有居于中心的优势.意味着多个发件人可以发送相同的事件,而发送给侦听器的发送者却没有区别...

This has the advantage of being central. Meaning multiple senders can send the same event, and to the listeners it doesn't make a difference who sent it...

我在博客文章中对此主题进行了扩展.

这篇关于PHP Event-Listener最佳实践实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆