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.

3

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.

0

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();
    }
}
  • 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 May 7 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 May 7 at 8:07
  • It is always a developer Call. But for future perspective, you should use multiple observer class. – Amit Bera May 7 at 8:09
  • @Marius what you have chosen? – Amit Bera May 9 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 May 9 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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