4

I have a piece of custom code that should be executed on different events but the code does not depend on anything the events send to the observer.
It's basically a dumb code like:

class MyObserver implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        do something here that does not depend $observer
    }
}

Is there a downside in using this for multiple events like this (events.xml)?

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="event_one">
        <observer name="my_observer" instance="MyObserver" />
    </event>
    <event name="event_two">
        <observer name="my_observer" instance="MyObserver" />
    </event>
    <event name="event_three">
        <observer name="my_observer" instance="MyObserver" />
    </event>
</config>

I can also create an abstract class that contains that code and create empty classes for each event, but I'm trying to avoid abstract classes and empty child classes.

4 Answers 4

4

Logically I don't see any benefit of creating multiple classes for same functionality. Moreover having same class it will help in code redundancy, upgrading and also debugging.

In case of Plugin you should have different class defined even if the functionality is same . But in case of observer it would be better if you use the same class in my opinion.

2

As per your requirement, you have mentioned that the functionality of the observer has nothing to do with the observer argument and is independent.

In this case, according to me, the best solution is to create and call a single observer file and function.

The main benefit of it is that if in future there are some changes, change needs to be in one single file rather then changing in multiple files.

Also using abstract class will not have any advantage as the primary functionality of observer is independent, so if you create an abstract class and individual child classes for multiple events, there is no usage of child classes.

Overall, i cannot see any disadvantage of using single observer class, so it is recommended by me.

1

In my option, it is the best idea to a create Class on Model and also define a Method.

After that, you have to call that method on your observer classes.

Model Class:

 namespace {Vendor}\{ModuleName}\Model;
 class {MyClass}
 {
  public function myFunction()
  {
   // do whatever you need
  }
}

After that called myFunction() on MyObserver using injection.

<?php
namespace {NameSpace};

class MyObserver implements \Magento\Framework\Event\ObserverInterface
{

    private $MyClass;

    /**
     * Constructor
     *
     * @param \{Vendor}\{ModuleName}\Model\{MyClass} $MYClass
     */
    public function __construct(\{Vendor}\{ModuleName}\Model\{MyClass} $MYClass)
    {
        $this->MyClass = $MYClass;
   }    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $this->MyClass->myFunction();
    }
}
5
  • 1
    I already have that. That's what my observer does. It calls the code from one of my classes. My question was about the observer itself. Should I used it for multiple events or create one observer class for each event?
    – Marius
    Commented May 7, 2019 at 7:58
  • I guess that you want to know do you want to use single observer Or multiple observers Class. If that is then I suggest using Single Observer class
    – Amit Bera
    Commented May 7, 2019 at 8:07
  • It is always a developer Call. But for future perspective, you should use multiple observer class.
    – Amit Bera
    Commented May 7, 2019 at 8:09
  • @Marius what you have chosen?
    – Amit Bera
    Commented May 9, 2019 at 7:58
  • I ended up using the same observer class for multiple events. it looks like it works. But the story in which I need this is still in development. QA will check for side-effects later.
    – Marius
    Commented May 9, 2019 at 8:24
0

I would change the naming of the observers to be more explicit (like Vendor_Module_EventX_MyAction) (might be helpful when debugging) and use the same class instance for all of them

1
  • thanks for the suggestion, but this does not answer the question. Anyway, the names are used as examples. they are not real names.
    – Marius
    Commented Jun 19, 2023 at 14:15

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.