Take the 2-minute tour ×
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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.

share|improve this answer
1  
and community if you are giving something back :) –  David Manners Jul 2 at 13:29
    
I agreed the same but one of my friend said it will be a problem while upgrading, please suggest with reasons how? –  TBI Infotech Jul 2 at 13:29
    
Refer this to check what i want to say:magento.stackexchange.com/questions/25743/… –  TBI Infotech Jul 2 at 13:31

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.

share|improve this answer
1  
This is the best answer ever. –  kab8609 Jul 2 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) –  TBI Infotech Jul 2 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 Jul 2 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. –  Paul Hachmang Jul 2 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. –  Paul Hachmang Jul 2 at 14:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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