I writed a preference for class Magento\Catalog\Helper\Image but i got this error:

Recoverable Error: Argument 4 passed to Magento\Checkout\Block\Cart\Sidebar\Interceptor::__construct() must be an Instance of Magento\Catalog\Helper\Image, instance of RMI\ImageCDN\Helper\Image given, called in C:\xampp\htdocs\magento\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php on line 107 and defined in C:\xampp\htdocs\magento\var\generation\Magento\Checkout\Block\Cart\Sidebar\Interceptor.php on line 11

If preference is not a good way to override a helper in core , what is the solution?

1 Answer 1


you can do it this way, but you have to extend the original helper, because all classes that use it, actually expect an instance of the original helper (or a derived class)

  • The preference way got a error Recoverable Error: Argument 4 passed to ... It seems magento dose not inject my preference class instead original class in all constructors in core because the error point that : Magento\Checkout\Block\Cart\Sidebar\Interceptor constructor must be an Instance of Magento\Catalog\Helper\Image, instance of RMI\ImageCDN\Helper\Image, the "RMI\ImageCDN\Helper\Image " is my preference class
    – ali gh
    May 29, 2016 at 20:14
  • the error message says, that the constructor of Magento\Checkout\Block\Cart\Sidebar\Interceptor expects an instance of the original class (Magento\Catalog\Helper\Image), but, because of your preference, an instance of your new helper class is injected. Since the constructor expects Magento\Catalog\Helper\Image, you have to extend your class from it. May 29, 2016 at 20:18
  • sound is like good, I did mypreference(RMI\ImageCDN\Helper\Image) extends Magento\Catalog\Helper\Image but new error happend: Recoverable Error: Argument 2 passed to Magento\Catalog\Helper\Image::__construct() must be an instance of Magento\Catalog\Model\Product\ImageFactory, none given, called in C:\xampp\htdocs\magento\app\code\RMI\ImageCDN\Helper\Image.php on line 145 and defined in C:\xampp\htdocs\magento\vendor\magento\module-catalog\Helper\Image.php on line 136
    – ali gh
    May 29, 2016 at 21:10
  • you have to call the original constructor with the original arguments (like the error states) of course. However, this is a new problem so please add a new question for this May 29, 2016 at 21:14
  • OK , Thank u for your help, I will ask a new question.
    – ali gh
    May 30, 2016 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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