· 2 min read
Magento: conventions and event-driven programming
Some thoughts on using events in Magento and PHP instead of overriding the core in extensions.

Episode rescued from the SeViR Notes archive and translated from Spanish. Originally published on October 15, 2015.
Plenty of platforms have moved to event-driven programming. In a watered-down form, though, since it isn’t always what we think of as events that fire and run in the background.
I’ve spent a few intense weeks developing on Magento to wrap up a project. Like WordPress, Magento has a set of events you can hook your own routines into, so they run whenever those events are fired.
Unlike in JavaScript, Node, C# and others, events in PHP run sequentially, and they’re basically a way to run bits of code in the middle of the application’s main flow without having to touch its core. A class acting as the event dispatcher stores all the callback functions alongside the event they should run on, and calls them one after another when that event is triggered.
It’s often easy to see that this isn’t the style of programming we’re used to in PHP, and I’ve seen loads of extension code that chooses to override everything rather than use events, sometimes breaking the functionality of other extensions in the process.
Whenever I work with free software and purchased third-party extensions, I get the feeling that, even though the functionality is there, the code is sometimes far from good, and ends up being a real headache for every other developer.