latvian
latvian

Reputation: 3215

Why can i not rewrite parent class?

I want to rewrite core/adminthtml/block/widget/tabs.php class which is a super class for core/adminhtml/block/sales/order/view/tabs.php class.

Here is config code for both scenarios:

 <blocks>  
  <adminhtml>  
     <rewrite>
       <sales_order_view_tabs>Jimmy_SalesAffil_Block_Widget_Tabs</sales_order_view_tabs>  
       <widget_tabs>Jimmy_SalesAffil_Block_Widget_Tabs</widget_tabs>  
        .....
     </rewrite>  
  </adminhtml>  
 </blocks> 

While I am able to rewrite ...view/tabs.php, I am not able to rewrite the super class. Why? Is it possible? How?

Upvotes: 2

Views: 1520

Answers (2)

Alana Storm
Alana Storm

Reputation: 166086

You're right, you can't, and it has to do with the way the override functionality was created. PHP (unlike say, ruby) offers you no native way to override classes. Magento works around this by (for overridable classes) never using the new keyword

new ClassName

to create a class

Instead they use a static factoryish method

$object = Mage::getModel('/adminhtml/sales_order_view_tabs');

What get model does is look at the string /adminhtml/sales_order_view_tabs, and then reference the config files to see which class it should instantiate.

Because parent classes are never instantiated, they can never be overridden.

Upvotes: 2

Joe Mastey
Joe Mastey

Reputation: 27119

You can rewrite the parent class, but that will only apply to calls for that class itself. So this call will retrieve the correctly overridden class:

Mage::getBlock("adminhtml/widget_tabs");

This is because the real classname is loaded by Magento using the XML / overrides system. However, if you don't override the child class, this call will not work as you expect:

Mage::getBlock("admihtml/sales_order_view_tabs");

That's because the parent class is not specified using Magento's system, but using regular PHP:

class Mage_Adminhtml_Block_Sales_Order_View_Tabs extends Mage_Adminhtml_Block_Widget_Tabs

If you want to override the parent class, the XML-based override system cannot help you. You can, however, copy the Widget_Tabs class into the local space and Magento will load it there. Make a directory path app/code/local/Mage/Adminhtml/Block/Widget/ and copy Tabs.php into it, and you can make modifications as necessary.

Hope that helps.

Thanks, Joe

Upvotes: 3

Related Questions