Override/Extend Magento Abstract class?

Michael picture Michael · Apr 1, 2013 · Viewed 8.7k times · Source

I only need to add a method to the Mage_Core_Model_Abstract class.

For that, in my config.php I have

<models>
     <mymodule>
         <class>Mynamespace_Mymodule_Model</class>
     </mymodule>
     <core>
         <rewrite>
             <mage_core_model_abstract>Mynamespace_Mymodule_Model_Abstract</mage_core_model_abstract>
         </rewrite>
     </core>
</models>

It doesn't really work..

I know that there is a way of "overriding" by copying all the folder structure including the class file into my module, but I would like to know if there is a way to make it more fancy, within my module - something similar as above..

Answer

Nick picture Nick · Apr 1, 2013

You can only rewrite classes that you instantiate directly, from config.xml.

The various values in config.xml are used when creating an object through a factory (e.g. Mage::getModel($class), Mage::getSingleton($class), Mage::helper($class), etc). The value of the $class argument is used to translate catalog/product into Mage_Catalog_Model_Product.

This means it's impossible to rewrite (in the Magento sense) classes that are used as superclasses, including abstract classes (by definition).

If you want to redefine any class that's used as a superclass, then you'll need to place a file in the correct place further up the include path. In this case, you'd need to make a file in app/code/local/Mage/Core/Model/Abstract.php.