2

I was just reading article on "Understand Overriding in Magento: Models".

And one of the comments was that:

Overriding Magento blocks, models, helpers and controllers is little dirty. My suggestion it to use events.

The author of the article agreed to it:

I agree with you completely. Yes, as you've mentioned the first choice should be the events and if it's not possible through events, you should go for overriding. In fact, I had this specific use case (something which can't be done with event) in mind while I wrote this.

But there were no explanation. Please explain why events are better than overriding.

And if possible provide easy example which can be done either using events or overriding (with code snippets), where events will be preferred solution.

1

I would definitely suggest event instead of overriding core class. for example If you want to do operation on after adding product into cart then you will surely look into addProduct method from Mage_Checkout_Model_Cart you can see from this link.

On this method you can find one event checkout_cart_product_add_after. If you use this event then you can easily get the quote_item (by $observer->getEvent()->getQuoteItem()) object so you can do your manual operation also you don't need to worry about magento add to cart process and If magento team upgrade the version like 1.9.3 to 1.9.4 or so on. You will always get the quote item object.

But, If you override the core class then there might be case in future version of magento your code won't work because they have added some more custom operation for this method and this operations does not exist in your override class (of course you have method code from previous version of magento)

So always use event if its possible

  • Keyur, thank you for your reply. "for example If you want to do operation on after adding product into cart" - what is example of such operation? Basically I need small test task to give to magento developer candidate I am considering to test him. I already have list of small test tasks which tests different aspects of magento understanding. This one will be to test that he understands that it’s better to use events instead of overrides. – MployBy Oct 22 '17 at 14:00
1

As @Keyur pointed out overriding class always has a risk of breaking functionality in the future release. As a code developer you need to test your code with the new base code releases and adjust it. It may happen so that the change in the core code is of a nature that requires you to prepare 2 different logic for your customization in the class you are overriding depending on the core version.

Another thing to consider is the fact that overriding a class is a winner-takes-all strategy. Since there is no way to make 2 different modules simultaneously override the same file only 1 definition will be used. This means that when you are overriding any class in your code you are (1) making it impossible for anyone else to do the same and (2) you are at risk of having your code not working because some other module definition is used in a particular case than yours. I have seen many cases where the store would purchase 2 modules that were to add some functionality to the products and both of them would override Mage_Catalog_Model_Product or Mage_Catalog_Model_Resource_Product class. This make one of them not working and costing the store additional developer, like me, time of going through both module code, understanding the logic of the extension, creating 3rd module that would win the battle of overwriting and combine those 2 module logic.

On the other hand events deal with all those problems by making an easy access point to the external modules without the risk of being in conflict. Of course 2 modules may cancel each other results out by hooking into the same event and redefining the same element (ie. shipping price) but this is rather rare as stores won't use n different modules to do the same thing. Even when things like that occurs it is far easier to track and fix than tracking why some overwrite is not working as expected.

To sum up using events where possible is good for developers, both integrators and extension makers as it allows their code to less dependent on the store setup and magento version. It is also better for the store because it lowers the possibility of the additional development need to integrate the bought module into the store.

  • Zefiryn, thank you for your reply. Could you provide example of simple task that can be both acommplised by events as well as overrides. Basically I need small test task to give to magento developer candidate I am considering to test him. I already have list of small test tasks which tests different aspects of magento understanding. This one will be to test that he understands that it’s better to use events instead of overrides. – MployBy Oct 22 '17 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.