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.
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Sheldon\Akeneo_Connector\Job\Category"> <arguments> <argument name="configHelper" xsi:type="object">Sheldon\Akeneo_Connector\Helper\Config</argument> </arguments> </type> <preference for="Akeneo\Connector\Job\Category" type="Sheldon\Akeneo_Connector\Job\Category" /> </config> |
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.