2

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() ?

5

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'));
2

it is should be possible to do it this way:

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)
       Varien_Object::_afterSave(); // looks like static call but it will call a grandparent
       return $this;
   }
}
8
  • Take note of the question. This is not longer valid > php5.4 - it will throw a STRICT warning. – ProxiBlue Nov 5 '15 at 1:58
  • No, it does not, I'm using it all the time: 3v4l.org/hGZ2l – Fabian Schmengler Jul 19 '16 at 6:45
  • Explanation: the strict warning comes only if $this is not an instance of the referenced class name – Fabian Schmengler Jul 19 '16 at 6:46
  • I see from your question that you actually were aware of that - maybe the strict warning had a different reason in your case? It really should work on any PHP version – Fabian Schmengler Jul 19 '16 at 6:50
  • @FabianSchmengler Been a log time since I came across this very same issue (again), just curious if you still 'continue to use it' ongoing? Deprecated functionality: Non-static method Mage_Core_Block_Abstract::_beforeToHtml() should not be called statically, assuming $this from incompatible context which was for using return Mage_Catalog_Block_Product_Abstract::_beforeToHtml(); in a custom module – ProxiBlue Jan 18 '18 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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