Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento duplicate class rewrite

I'm using two different Magento modules, say First_Module and Second_Module which override the same class. I would like to combine their functionality.
My assumption was that I could extend the duplicate class from First_Module from the same class in Second_Module which in turn extends the original class. I would guess that to do this, First_Module should depend on Second_Module, so I added to the module's config <depends>Second_Module</depends>.

I would expect now the configuration loader to load Second_Module first and that its no longer valid tag for the duplicate class would be overridden by the same statement in the config of First_Module. But this doesn't seem to be the case. The first rewrite seems to be used.

Am I doing/understanding something wrong or is this just how it works? Maybe I should just remove the rewrite from the second module from the config (which I rather not do to keep it as original as possible).

Thanks in advance.

like image 948
Lucas Moeskops Avatar asked Mar 18 '11 13:03

Lucas Moeskops


2 Answers

The Magento class autoloader is not made for multiple overrides of one class because that concept actually wouldn't make any sense. There would be no way to enforce that the second override respects the first one, and so they did not implement it at all.

Additionally, you cannot usually rely on having two separate modules installed (outside of the core modules), so your ability to accommodate other overrides would be fragile at best anyway.

If you want to accomplish this, look first to Magento's event delegation system. Many listeners can operate on one event, so this may be a natural case for you.

Additionally, as someone else pointed out in a question recently, you could create two subclasses of the same class and use each of those rather than the original. This will not allow you to modify existing system behavior, but if you simply need extensions to the core classes it may be sufficient. In this case, you would be calling those classes directly (as in Mage::getClass("mymodule/extended_customer");.

Hope that sheds a little light on the situation.

Thanks, Joseph Mastey

like image 164
Joe Mastey Avatar answered Nov 12 '22 16:11

Joe Mastey


Just as an aside to answer from Joseph,and could save you a lot of trouble, you can simply make the second rewrite class ( the one not seen by magento rewrite system ), be the ancestor of the rewrited seen clas ( the first read config as pointed by Joseph ) so if both class doesnt affect the very same methods, you will end having both functionalities and one rewrite..

I do that way routinely for "Universal Customer Password" extension that my customers find really useful, but happen to rewrite customer model, and based on experience, it's a customer is a class that it's rewrited more than often by many extensions commercial or not..

like image 1
HippoMan Avatar answered Nov 12 '22 15:11

HippoMan