what's the proper way to use Symfony's EventDispatcher component?

后端 未结 2 1370
清歌不尽
清歌不尽 2021-02-04 15:01

I\'d like to promote loose coupling in my PHP code, by making certain classes observable. Symfony\'s EventDispatcher component looks promising, as does the SPL

2条回答
  •  遥遥无期
    2021-02-04 15:25

    I would avoid (2). Inheritance is possibly the most over-used pattern and probably isn't relevant here. The choice between options (1) and (3) probably depends on your context. Whilst avoiding tight coupling is good, you should be wary of swiss-army knife solutions. Having an eventDispatcher that's everywhere in your app and does almost everything can make your code harder to test/understand/maintain etc (it can approach a God object). On the other hand, the Spl solution is much simpler and hence if you do need multiple observers/observables you might find you are having to maintain too many SplObservers/SplSubjects.

    As with most things in OOP, there is no best way, and will normally depend on your exact use cases...

提交回复
热议问题