Magento Dev Best Practices: Event Observers

This is the fourth post in the Magento Dev Best Practices series. If you don’t know about and use Event Observers already, make it a priority to learn them. Throughout Magento, there are a lot of events that are dispatched at different times. Some are specifically named, others are more dynamically named (model save before and save after events for example). Most of these events, when dispatched, provide one or more objects in them. All of these events can be observed, and the objects provided in them can be modified.

The best practice here is to follow this rule: Do not ever rewrite a core file without first making sure there is no possible way to accomplish the functionality with an event observer.

The reason for this is, as you probably already know, that you cannot rewrite the same core file more than once (without creating an extends chain). By using event observers instead, multiple modules can exist and be changing functionality all at the same time without conflicting. Technically, any number of modules can observe the same event and make changes. Conflicts can still occur, but it’s far more rare.

A fairly good list of events can be found here, but do take note that there are a lot of dynamic events not listed here. All models have a save_before and save_after for instance.

I am not going to give a tutorial on how to use them here, but if you don’t know how to use observers in Magento, then google it and learn it. It is incredibly worth knowing, and it will take your development to the next level and improve the quality and compatibility of your modules.

This entry was posted in Magento. Bookmark the permalink.

Comments are closed.