1

I recently fell into an interesting argument.

My question is when should we use the local codepool to completely overwrite a class and when should we use Magento class overrides extending a class.

What are the pros and cons of using local copy over rewriting a class.

2 Answers 2

5

That is an easy one: you should never use core, but always local.

Or did I misunderstand the question?

Update:

Reasons are simple as well. If you overwrite files in core and then update your shop (i.e. update the core folder) to a new version, all your modifications will be lost and you will need to search all the files if you have modifications.

In contrast, when you have modifications in local, you know which files are edited and can diff them to the new files.

However, the best case is to use rewrites in your own custom module, so you do not need to completely replace a file and can just modify it to your needs.

Second update:

Your question really is why you should avoid overwriting in local and instead use a rewrite!?

If so: It is better to do a rewrite than put the file in local, since chances are that your rewrite will work with a new version of Magento while your local file won't. Why is this?

Let us assume you want to overwrite class A which has 100 methods. You only need to modify one method let's call it "testMethod()".

So in your rewrite, you will extend from the core class and only implement that one method. In contrast, when copying it to local and then change that method, you have overwritten the whole file (so 100 methods).

Now you update your shop and the class A now has 140 methods (40 new methods created by Magento Core team). Your rewrite still rewrites only one method, so all 139 other methods are preserved. However, your local file still has 100 methods, so 40 methods are missing...

So a rewrite has the potential to still work after an update - it could be that the method you are overwriting still needs to be modified, but chances are everything still works.

3
10

Here is the order in which you should customize core features.
Start from the top and if one option is not possible go to the next one.

  1. Observers
  2. Class rewrites
  3. Copy class to local folder.
  4. Edit the core - Do this only if you want the universe to implode on itself.

The observers approach is the safest. Observers usually don't disappear on an upgrade and they act like a black box. You always know what goes in and what should come out. And you can easily remove it when not needed.

The class rewrites is a bit riskier that the observers approach. In the event of an update some class methods might be changed and if you changed that method you might lose the functionality added by the upgrade or even worse..."white screen of death". But since not everything is observable you can choose this. Here is an example that is killing me. When sending an e-mail there is no observer to pass additional template vars to the e-mail template.

Copy class to local is risky as "h..eck". when an upgrade is done, if something in the class changes ...well you are on the same case as the one above but with a higher chance of happening. You should use this only if you need to change something in abstract classes, or classes that are never instantiated. More words about it here. http://marius-strajeru.blogspot.ro/2011/09/why-cant-you-override-some-classes-in.html

Core modifications...if you do this you deserve what's coming. I speak from experience.

7
  • 1
    This is the best answer ever.
    – kab8609
    Commented Jul 2, 2014 at 13:33
  • @Marius: I agree, but please check the link I shared, I guess you know what I am pointing at ;) hence i posted a question straight away (No offenses) Commented Jul 2, 2014 at 13:37
  • 1
    @TBIInfotech. I understand what you mean...that's why I added the observers approach in here also, even if you didn't mention it in the question. ON the question you linked, I think the safest way to do it is exactly how the OP answered himself. The risk is minimum. It will fall over only if the addTab method signature changes. and I doubt that.
    – Marius
    Commented Jul 2, 2014 at 13:40
  • to expand: using the local code pool to override a class, will always (!) result in code dept and is by definition a dirty hack. There are some exceptions (modifying the xml config class for example) where you have no other choice. Commented Jul 2, 2014 at 14:25
  • Also, never rewrite very important classes like the Mage_Sales_Model_Order (and invoice, etc.) classes, cart classes. Use observers exclusively here. Commented Jul 2, 2014 at 14:26

Your Answer

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

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