I want to add my custom function in Magento\Framework\Message\Manager file in core file its working perfectly, but when I'm trying to override its not working. So, I want to know how to override class that implements interface in magento2.

below file I need to override:


1 Answer 1


You can override a class that implements an interface, the same way that you would normally overwrite a class. But you'll have to choose how you want to implement the interface.

In either case, first you'll have to declare your override as a preference in your module's etc/di.xml

<preference for="Magento\Framework\Message\Manager" type="Vendor\MyModule\Plugin\Magento\Framework\Message\Manager" />

Then the next step is to create Vendor\MyModule\Plugin\Magento\Framework\Message\Manager.php. Then choose one of the possibilities.

Method #1: Extending the original class (keeping the interface implementation in the original file)

By extending the original class you can overwrite only specific methods or add your own.


namespace Vendor\MyModule\Plugin\Magento\Framework\Message;

class Manager extends \Magento\Framework\Message\Manager {

    public function myFunctionHere() {



Method #2: Completely overriding the original class + interface

This one is more complex as you'll have to reimplement the whole class. Not something that I can recommend doing.


namespace Vendor\MyModule\Plugin\Magento\Framework\Message;

use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\Message\MessageInterface;
use Magento\Framework\Message\Session;
use Magento\Framework\Message\Factory;
use Magento\Framework\Message\CollectionFactory;
use Magento\Framework\Message\ExceptionMessageFactoryInterface;
use Magento\Framework\Message\ExceptionMessageLookupFactory;

use Magento\Framework\Event;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Debug;

class Manager implements ManagerInterface {

    ... /* Vars */

    public function __construct(
        Session $session,
        Factory $messageFactory,
        CollectionFactory $messagesFactory,
        Event\ManagerInterface $eventManager,
        LoggerInterface $logger,
        $defaultGroup = self::DEFAULT_GROUP,
        ExceptionMessageFactoryInterface $exceptionMessageFactory = null
    ) {
        $this->session = $session;
        $this->messageFactory = $messageFactory;
        $this->messagesFactory = $messagesFactory;
        $this->eventManager = $eventManager;
        $this->logger = $logger;
        $this->defaultGroup = $defaultGroup;
        $this->exceptionMessageFactory = $exceptionMessageFactory ?: ObjectManager::getInstance()

    ... /* Other methods */

  • Thanks its working for me @sduif .. Commented Oct 14, 2019 at 6:23

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.