I seem to be struggling to grasp Magento 2 dependency injection with blocks, every time I try to extend a block that isn't \Magento\Framework\View\Element\Template I end up with errors.
I want to create a block that extends the very basic block class of Magento\Theme\Block\Html\Header\Logo - everything works fine until I try dependency injection within the construct method:
<?php namespace Creare\Test\Block\Header; class Logo extends \Magento\Theme\Block\Html\Header\Logo { protected $_creareHelper; public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Creare\Seo\Helper\Data $creareHelper, array $data = [] ) { $this->_creareHelper = $creareHelper; parent::__construct($context, $data); } } As soon as I try to inject my helper class (or anything else for that matter, I get a stack trace starting with the following error:
Recoverable Error: Argument 2 passed to Magento\Theme\Block\Html\Header\Logo::__construct() must be an instance of Magento\MediaStorage\Helper\File\Storage\Database, array given, called in /Users/adammoss/PhpstormProjects/Magento2/app/code/Creare/Test/Block/Header/Logo.php on line 17 and defined in /Users/adammoss/PhpstormProjects/Magento2/app/code/Magento/Theme/Block/Html/Header/Logo.php on line 33 If I add the same dependencies to my __construct as the file I'm extending from it works, but surely that's a backwards way of doing things as the point of class inheritance is that I absorb all of the parent's methods and properties?
I think I just need a basic 101 explanation from someone on extending from classes and using DI with Magento 2. Any help much appreciated!