Override classes and constructor arguments

Override Classes Magento2

As for Magento 1 it is possible to override classes in Magento 2 as well. Don’t get me wrong. It is not recommended to override classes or methods. It should be the really last resort. Use a plugin or an observer whenever possible. But sometimes the code you’re working against isn’t suitable for one of theses prefered methods. So you have to use a class override.
An override can be easily achieved by adding some lines of xml to the di.xml of a module. 

In this snippet the class Akeneo\Connector\Job\Category is overridden by Sheldon\Akeneo_Connector\Job\Category.
Additionally the inherited parameter configHelper is overridden with another object class (\Sheldon\Akeneo_Connector\Helper\Config).

Further information about the dependency injection mechanism can be found in the official Magento 2 docs under https://devdocs.magento.com/guides/v2.4/extension-dev-guide/build/di-xml-file.html

More snippets round about Magento2 can be found here.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.