1

I often find it annoying that I have to repeat all of the dependencies after extending a class. Am I missing a trick here?

For example the Block Template class only requires that you inject and pass two parameters to the parent. Would I have to create my own context object if I wanted to do something like that?

  • That's the point of inheritance, what exactly are you trying to do? What do you need to extend from the Block Template class? – André Ferraz Nov 8 '16 at 12:36
  • I extend the template class into a block for my module, then I extend that block from another more specific block. I don't want to have the DI in its full form twice, it's too verbose. I'm aware of what inheritance is meant for, I was wondering if there was a Magento trick like the way they pass context and data alone. – LM_Fielding Nov 8 '16 at 13:26
  • Yes, but what exactly in the Template block class are you extending/changing? – André Ferraz Nov 8 '16 at 13:30
  • Your question is irrelevant, I'm adding custom functions and injecting more classes. – LM_Fielding Nov 8 '16 at 14:04
1

I have a similar need and wish if I could use params in _constructor instead (note single _, there is a __constructor and _constructor in Magento). Unfortunately it ends up with _construct should be compatible with parent _construct anyway.

It is possible to use objectManager as a not well optimized workaround.

/**
 * Model construct that should be used for object initialization
 *
 * @return void
 */
protected function _construct()
{
    $this->emailConfig = \Magento\Framework\App\ObjectManager::getInstance()->get(\LCB\ContactForms\Model\Email\Template\Config::class);
    parent::_construct();
}

An answer why to do that is obvious. You don't have to worry that you constructor will be not compatible between various Magento 2 versions. Magento2 plugin is way better solution instead class rewrite but still, you can't modify protected methods with it.

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.