2

I want to know with a pretty explanation the difference between the different types of Observers bellow:

1)

<?php 
class Company_Module_Model_Observer
{
    public function someFunction($observer)
    {
        ...
    } 
}

2)

<?php 
class Company_Module_Model_Observer extends Varien_Event_Observer
{
    public function someFunction($observer)
    {
        ...
    } 
}

3)

<?php 
class Company_Module_Model_Observer
{
    public function someFunction(Varien_Event_Observer $observer)
    {
        ...
    } 
}

And also the difference in the xml declaration between <type>singleton</type> and <type>model</type>, in which case we should use each one.

PЯINCƎ
  • 11,669
  • 3
  • 25
  • 80

1 Answers1

7

1 and 3 are the same thing.
The argument passed to a method that acts as an observer to an event is always of type Varien_Event_Observer.
I recommend the last one. It makes it more clear for the person that reads/writes code after you what type of parameter is that.

I haven't seen any occurrences of case 2. I don't see a reason for your observer class to extend Varien_Event_Observer.

As for type tag, it determines how your observer class will be instantiated. if type is singleton and your observer class observes multiple events, the same instance of the class will be used for all of the events. It saves some memory but it is dangerous. You can mix things that may not need mixing.
If type is model, then each time your observer "observes" an event, a new class of your observer will be created.
You can see some additional words about singleton vs model in here: Magento Event Observers: Singleton versus Model

[EDIT]
From Rick Buczynski's comment below:
At least for general events that get dispatched amultiple times on the same request, like core_block_abstract_to_html_before it would be better to use singleton to save some memory.

Marius
  • 197,939
  • 53
  • 422
  • 830
  • 2
    Why even bother to write my answer when Marius is already done? :-p – Rick Buczynski Sep 18 '17 at 21:30
  • @Marius i forgot another one, sometimes i see this: class Company_Module_Model_Observer extends Mage_Core_Model_Observer it is not necessary right ? – PЯINCƎ Sep 18 '17 at 21:42
  • you don't really need for an observer to extend something. Unless you really really need a (protected) method from a different class, but I doubt you will. – Marius Sep 18 '17 at 21:43
  • @Marius okey, I would not do it since you confirm it to me, the last thing about the type observer tag, If I understood correctly if we observes multiple events, we must use <type>model</type> for the reasons you explained to me, and if I use all the time model, this is a good practice, at least I avoid danger ? – PЯINCƎ Sep 18 '17 at 21:54
  • yep. Use model by default, until you find a specific case that requires a singleton – Marius Sep 18 '17 at 22:09
  • Ok, thank you very much, it's always a pleasure to have your clear answers and precise. – PЯINCƎ Sep 18 '17 at 22:13
  • 1
    Personally I prefer to use a singleton type, unless a model is needed. In my experience, an observer is often a utility that is stateless, and as such can work well with less memory footprint if served via singleton. This is especially true when the event you're observing is called many times; ie: core_block_abstract_to_html_before ought to be observed by a single instance if possible. – Rick Buczynski Sep 18 '17 at 22:57
  • That's actually a valid point. – Marius Sep 19 '17 at 04:49