Question

[TL:DR]
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?.

Was it helpful?

Solution

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.

inheritance

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top