0

I have installed two third-party modules and one local module. I want to rewrite/ extend the Helper/Data.php of both third-party modules. Currently, I'm trying to do this from my local module's Helper/Data.php.

My question, is it possible to have my single Helper/Data.php extend both of the third-party helpers? So far, this is not working. What would be the best approach to this? I know I could fix this by creating another local module to rewrite the other third-party module, but since the functionality is related I'd rather stick to a single local module to accomplish it.

To add some detail, this is my rough setup:

MyCompany_Module/etc/config.xml:

    <helpers>
        <moduleb>
            <rewrite>
                <data>MyCompany_Module_Helper_Data</data>
            </rewrite>
        </moduleb>
        <modulea>
            <rewrite>
                <data>MyCompany_Module_Helper_Data</data>
            </rewrite>
        </modulea>
    </helpers>

and in my module's MyCompany_Module_Helper_Data:

class MyCompany_Module_Helper_Data extends PartyA_ModuleA_Helper_Data 
{
   ...
}

class MyCompany_Module_Helper_Data extends PartyB_ModuleB_Helper_Data 
{
    ...
}

So I am trying to extend two Helpers from a single Helper, is this possible/ an accepted approach? What is the generally accepted approach? Should I split this into a MyCompany_Module_Helper_Data1 and MyCompany_Module_Helper_Data2, or even MyCompany_Module1_Helper_Data and MyCompany_Module2_Helper_Data

Thanks

2
  • I'm confused, are you actually putting 2 class definitions in the same file? Jan 5, 2015 at 23:22
  • Yes, that's what I tried to do. I suppose that is not the possible, since I am trying to define the same class twice right? The fact that I have each extend another Helper doesn't change that. What would be the best approach? Two separate helpers in the same module?
    – Martin
    Jan 5, 2015 at 23:29

2 Answers 2

2

As far as I know, this is not at all possible with magento. Basically what you need is to inherit your class from HelperA (of module A) and HelperB (of module B).

In php, you cannot extend a class from two base classes

So if you really need to extend from HelperA and HelperB at the same time, then the best shot would be..

Extend your your Helper from HelperA and then rewrite HelperA class in such a way that it extends from HelperB instead of default base class Mage_Core_Helper_Abstract

OR

Extend your your Helper from HelperB and then rewrite HelperB class in such a way that it extends from HelperA instead of default base class Mage_Core_Helper_Abstract.

This way your Helper will extend from both HelperA and HelperB. Which method should opt in this case is purely depends upon your application. So choose the right option. Here I am going to show you how to do the first implementation.

File : app/code/local/Namespace/Module/etc/config.xml

<config>
    <global>
        <helpers>
            <!-- your module's helper definition -->
            <namespace_module>
                <class>Namespace_Module_Helper_Data</class>
            </namespace_module>

            <!-- this part rewrites helperA class -->
            <helperA_alias_name>
                <rewrite>
                    <data>Namspace_Module_Helper_Rewrite_NamespaceA_ModuleA_Helper_Data</data>
                </rewrite>
            </helperA_alias_name>
        </helpers>
    </global>
</config>

File : app/code/local/Namespace/Module/Helper/Data.php

<?php
class Namespace_Module_Helper_Data 
    extends Namspace_Module_Helper_Rewrite_NamespaceA_ModuleA_Helper_Data
{
    //your helper method should come here.
}

File : app/code/local/Namespace/Module/Helper/Rewrite/NamespaceA/ModuleA/Helper/Data.php

<?php
class Namspace_Module_Helper_Rewrite_NamespaceA_ModuleA_Helper_Data
    extends NamespaceB_ModuleB_Helper_Data
{
    //you need to paste every single method of HelperB here.
}

Explanation

Here Namespace_Module stands for your module. NamespaceA_ModuleA stands for MODULE A and NamespaceB_ModuleB stands for MODULE B.

Your module's config.xml file holds two important definitions. First it defines your modules helper class Namespace_Module_Helper_Data. Secondly, it rewrites HelperA with a custom class Namspace_Module_Helper_Rewrite_NamespaceA_ModuleA_Helper_Data. This way magento will use Namspace_Module_Helper_Rewrite_NamespaceA_ModuleA_Helper_Data class when a request for helper class of MODULE A get processed.

Now see the class definitions. Firstly, your helper class extends Namspace_Module_Helper_Rewrite_NamespaceA_ModuleA_Helper_Data. This means your class is now inheriting Helper A. Secondly Namspace_Module_Helper_Rewrite_NamespaceA_ModuleA_Helper_Data extends from HelperB. Due to this now your helper class inherit from HelperB too.

ie Your helper class inherits from HelperA which then inherits from HelperB.

This is purely theory. I didn't try this before. But it seems really logical for me.

Note 1 : Use this approach only in the case, if you really want to define only one class which should extend from two base classes. Otherwise you can go for @Rick Method.

Note 2 : For the proper working of your module, you need to make sure two things here. First, your module should load only after MODULE A and MODULE B. Second, MODULE B should load before MODULE A. That is the order of loading should be..

  MODULE B - first
  MODULE A - second
  YOUR MODULE - third

Good luck

1
  • Thanks for the clear and elaborate explanation. Marked yours as the accepted answer because it more literally achieves what I asked for in the question. However, I ended up following @Rick's method.
    – Martin
    Jan 6, 2015 at 16:12
2

Your helper re-write XML is structured just fine, but Magento's namespacing convention and class-mapper does not handle multiple classes within a single PHP file. So, a better approach would be to split your classes into separate files, eg:

# File: app/code/local/MyCompany/Module/Helper/Partya.php
class MyCompany_Module_Helper_Partya extends PartyA_ModuleA_Helper_Data 

# File: app/code/local/MyCompany/Module/Helper/Partyb.php
class MyCompany_Module_Helper_Partyb extends PartyB_ModuleB_Helper_Data 

So you'll have to update your re-writes:

<helpers>
    <moduleb>
        <rewrite>
            <data>MyCompany_Module_Helper_Partyb</data>
        </rewrite>
    </moduleb>
    <modulea>
        <rewrite>
            <data>MyCompany_Module_Helper_Partya</data>
        </rewrite>
    </modulea>
</helpers>

Alan Storm provides a good primer on Magento's class lookup and auto-loading, which may give more insight into why this works:

http://alanstorm.com/magento_class_abstration_autoload

1
  • Thanks Rick, great answer. I ended up following your approach but marked programmer_rkt as the accepted answer as I feel it gives more explanation for future readers. I also read the blog by Alan and I've seen some of his answers here on other questions. Great information.
    – Martin
    Jan 6, 2015 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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