I have Core_Class_A and Core_Class_B extends Core_Class_A.
I need to rewrite both of them and add a common functionality.

So I made this: Custom_Class_A extends Core_Class_A.
The question is which one from below should I use (thumbs up and thumbs down for each):

  1. Custom_Class_B extends Custom_Class_A
  2. Custom_Class_B extends Core_Class_B

[Full Version] I'm working on an extension that adds a new filter in the layered navigation: in stock / out of stock (you will see it public in a few days).
The code, no big issue so far, the strategy...I have my doubts.
Because there are no events I can use in the layered navigation section to add my new filter, I had to rewrite 2 blocks: Mage_Catalog_Block_Layer_View and Mage_CatalogSearch_Block_Layer. (If someone has better ideas, please share, but this is not in the scope of the question).

For the Mage_Catalog_Block_Layer_View I did it like Vinai thought me indirectly (see this review about one of Vinai's extensions).
I added the methods I wanted to rewrite, just called parent::methodName() and dispatched an event that everyone can observe.

Here is an example for the method _initBlocks. My new method looks like this:

protected function _initBlocks()
    parent::_initBlocks(); //call the parent method
    //dispatch an event that I can observe and add my logic.
    Mage::dispatchEvent('catalog_layer_view_init_blocks', array('block' => $this));

But the problem comes for Mage_CatalogSearch_Block_Layer. This block extends the first one Mage_Catalog_Block_Layer_View.

For example I need to rewrite the method _initBlocks as shown above. But the problem is that Mage_CatalogSearch_Block_Layer does not have its own _initBlocks method. it uses the one from the parent class Mage_Catalog_Block_Layer_View.
What do I do here?
Should I use the same system as above or should I just make my new class [Namespace]_[Module]_Block_Catalogsearch_Layer extend my own class I created for rewriting Mage_Catalog_Block_Layer_View and just copy the different methods from Mage_CatalogSearch_Block_Layer?.

  • This is not opinion based if you state the advantages and disadvantages for each approach.
    – Marius
    Commented Nov 10, 2014 at 13:31
  • Traits would be the best approach IMHO
    – philwinkle
    Commented Nov 10, 2014 at 14:23
  • @philwinkle. I agree, but there is one flaw in your evil plan. Like I explained to Flyingmana for an answer below, this needs to work for PHP 5.3 also.
    – Marius
    Commented Nov 10, 2014 at 14:28

2 Answers 2


After having a look at your problem with this very basic class inheritance diagram (sorry for the bad drawing, by the way), I say it's better to continue with the approach you have taken and create Custom_Class_B extends Core_Class_B.


With regards to this drawing, it seems obvious to me that you will miss the extended functionality from Core_Class_B in version 1 (V1) and you would need to copy this functionality from the Core, which is not a good approach.

V2 is more transparent in this case.


First, you should never chose an approach where you need to copy methods of another class, if you have other choices. As soon as they change, you will get update problems and a log of work.

Extending them and implement your own method again is the better choice. Clearly you should not copy here again. Using an observer is a good way to avoid code duplication here, another would be the use of the PHP TRAIT Feature, which allows sharing code between classes.

  • Unfortunately I cannot use traits because this should work on php 5.3 also. Also some code has to be duplicated in my case. Either I duplicate the code from the core class, or from the one I created on step 1, when rewriting the first class. But I see what you mean. I think I should not duplicate code from core. Better my own code, because I have control over it. Thanks.
    – Marius
    Commented Nov 10, 2014 at 13:53

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.