Magento – Difference between overriding and extending or even rewrite (abstract class)

extendoverrides

I want to change the method collect() from

Mage_Sales_Model_Quote_Address_Total_Abstract

By what I've read it seems I can't rewrite it since it's abstract and it is never instantiate.

Looking around, the usual dirty trick is to use Magento autoload and copy the file and the exact path under /local/.
E.g.:

core/Mage/Sales/[...]/Abstract.php

to

local/Mage/Sales/[...]/Abstract.php

I also tried xml, but it didn't work (as expected) since you can't override an abstract.

<models>
        <sales>
            <quote_address_total>
                <rewrite>
                    <abstract>Lebernard_Dropship_Model_Sales_Quote_Address_Total_Abstract</abstract>
                </rewrite>
            </quote_address_total>
        </sales>
    </models>

Underlying my question, I feel I misunderstand this part of Magento. What is the difference between

  1. extending (without overriding) and how would you call the model in xml? Using :

    parent::collect($address);

  2. Using override in .xml (like above xml, rewrite)

  3. Using the autoload technique.

Thanks

Best Answer

Technically, you are correct, you can override the class in the local code pool, but you should not. When you override a class, you own it, meaning you must now maintain it. This includes upgrades, etc. It is very bad practice.

Second, since an abstract class is never instantiated through the abstract factory pattern, (Mage::getModel(), Mage::getSingleton(), etc) it will not work to rewrite it through configuration.

Third, when you rewrite such a low level class, you should seriously reconsider your approach. What are you trying to accomplish? Are you trying to rewrite the collect() method for over 24 classes? I ask because this is what Mage_Sales_Model_Quote_Address_Total_Abstract is inherited by. Perhaps you should rewrite a method call from a more specific class. Maybe check into an observer that will solve your issue.

To answer your questions listed above:

  1. Extending inherits the rewritten class and you only have to change the functionality of a particular method, (not the entire class).
  2. The xml above is rewriting a class. Putting items in the local code pool overrides them.
  3. Autoloading is a bit of a big topic, but simplistically:
    1. A class attempts to instantiate.
    2. The autoloader maps the class name to a file name, (example: Mage_Catalog_Model_Product maps to Mage/Catalog/Model/Product.php.
    3. Magento attempts to find this mapping in one of the three code pools (local, community, core) and in the lib directory.

A google search of autoloading in Magento will give more in depth process of autoloading.

Related Topic