Magento Class Inheritance – How to Call Grandparent Class

classinheritance

UPDATE:

This issue was finally tracked down as a class inheritance issue.
The original code in class becomes invalid, due to fact that the class inheritance has been updated, and the 'parent::parent' method is no longer a valid call.

Mystery solved. So simple, but somehow missed!

END UPDATE

This is more a PHP OOP related question, but I think falls within this community, since the question relates to magento and its class inheritance structure, and the way its core classes call parent methods to retain core functionality. If it is felt this does not belong here, I will happily remove this question.

I have of course googled this, and answers to this in stackexchange suggest changing the parent to place a flag to allow one to skip that class funcionality, but that woud mean having to rewrite the core classes, which is a no-go.

ref: https://stackoverflow.com/questions/8212048/parentparent-in-php
ref: https://stackoverflow.com/questions/1557608/how-do-i-get-a-php-class-constructor-to-call-its-parents-parents-constructor/1557663#1557663

As we all know magento has quite an inheritance scheme going, with inheritance running multiple classes deep.

Using the method _afterSave as an example, in the salesrule models:

CORE MODEL:

class Mage_SalesRule_Model_Rule extends Mage_Rule_Model_Abstract 
{
   protected function _afterSave()
   {
       .....
       core code here (that what I want to change)
       .....
       .....
       parent::_afterSave(); (end by calling the parent class method)
       return $this;
   }
}

CORE ABSTRACT PARENT CLASS:

abstract class Mage_Core_Model_Abstract extends Varien_Object 
{    
    protected function _afterSave()
    {
        $this->cleanModelCache();
        Mage::dispatchEvent('model_save_after', array('object'=>$this));
        Mage::dispatchEvent($this->_eventPrefix.'_save_after', $this->_getEventData());
        return $this;
    }
}

Ok, so now I extend Mage_SalesRule_Model_Rule in my own module, and I change the functionality, (I do not want to extend Mage_Core_Model_Abstract, as i want to inherit from Mage_SalesRule_Model_Rule) (note I am not doing a rewrite, I am just extending the core class from my own module class)

Thus in my class, I cannot call parent::_afterSave(), as I do not want the core class code to run that exists in my class's parent Mage_SalesRule_Model_Rule, but I do want to call the grandparent _afterSave() method, as I do not want to copy that code into my class.

Pre PHP 5.4, the grandparent class method could be called 'statically', thus it was easy to just do Mage_Core_Model_Abstract::_afterSave() (ref : https://bugs.php.net/bug.php?id=42016 – it is not a bug!)

However, since I upgraded to PHP5.4, this causes a STRICT warning, and I do not want to disable strict warnings.

So, finally the question:

How do others overcome this issue in magento modules, if not using Mage_Core_Model_Abstract::_afterSave() ?

Best Answer

Unfortunatly there isn't a very clean solution, but the best one I've come across is the following:

call_user_func(array(get_parent_class(get_parent_class($this)), '_afterSave'));