且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Doctrine 监听器与订阅器

更新时间:2021-11-04 22:52:50

在我看来,只有一个主要区别:

From my point of view, there is only one major difference:

  • 注册监听器,指定监听的事件.
  • 订阅者有一个方法告诉调度器它正在监听什么事件

这可能看起来没有什么大的区别,但如果你仔细想想,在某些情况下,你会想要使用一种而不是另一种:

This might not seem like a big difference, but if you think about it, there are some cases when you want to use one over the other:

  • 您可以将一个侦听器分配给许多具有不同事件的调度程序,因为它们是在注册时设置的.您只需要确保每个方法都在侦听器中就位
  • 您可以通过更改 getSubscribedEvents 的返回值来更改订阅者在运行时注册的事件,甚至在注册订阅者之后(想想有一次,您听到一个非常嘈杂的事件,只想执行一次)
  • You can assign one listener to many dispatchers with different events, as they are set at registration time. You only need to make sure every method is in place in the listener
  • You can change the events a subscriber is registered for at runtime and even after registering the subscriber by changing the return value of getSubscribedEvents (Think about a time where you listen to a very noisy event and you only want to execute something one time)

可能还有其他我不知道的差异!

There might be other differences I'm not aware of though!