27

I see some conflicting information on the internet and in 3rd party modules alike - is it a requirement or best practice to return $this at the end of an observer method?

E.g.:

MyCompany_Module_Model_Observer.php

public function salesOrderSaveAfter($observer){
    //do stuff
    return $this;
}

4 Answers 4

31

The core always return $this; in the context of observer methods - but there doesn't actually appear to be a reason for it.

Tracing back through dispatchEvent() you'll find the main method that calls observer methods (in ./app/Core/Model/App.php)

protected function _callObserverMethod($object, $method, $observer)
{
    if (method_exists($object, $method)) {
        $object->$method($observer);
    } elseif (Mage::getIsDeveloperMode()) {
        Mage::throwException('Method "'.$method.'" is not defined in "'.get_class($object).'"');
    }
    return $this;
}

But at no point is the return value ever actually used or referenced to be passed into another observer down the chain.

Perhaps Magento were thinking longer term to use it as some means to retain/pass data within $this class instance outside of using sessions/registry; or it could have been legacy code that has just stuck.

I can't see a compelling reason to return $this - but that being said, if they do it in the core, that's what we do.

As a general rule, whatever the core does - we deem best practice. With the exception of the shocking spelling mistakes :)

3
  • 6
    "Ukringlish" :-)
    – benmarks
    Feb 22, 2013 at 17:53
  • I am ashamed to admit that I sometimes return $observer to get rid of IDE warning of unused parameter... Feb 22, 2013 at 17:55
  • @Daniel - just don't add the param in the observer signature. PHP doesn't mind. But then again, the passed param might be needed in the future.
    – nevvermind
    Feb 22, 2013 at 18:04
8

$this (pun intended) is called a fluent interface. It allows you to call multiple methods within an object without having refer back to a defined variable.

2
  • 1
    Thank you - I understand fluent interfaces. I'm looking for a compelling reason to return $this when there is really no use for fluent interfaces on observers.
    – philwinkle
    Feb 22, 2013 at 18:03
  • 1
    @Kevin - You don't need a fluent interface in Mage's observers. Not that I know of.
    – nevvermind
    Feb 22, 2013 at 18:05
5

It's just a Magento convention to always return $this instead of void (nothing) if a method has no other return value, regardless of it actually being used for a fluent interface anywhere or not.

The advantage is, that you don't need to think about if it's useful or not, and a superfluous fluent interface is better than a missing one. Also, Magento might start using them for observers, even though this is highly unlikely.

0

Some years later ... :)

The core always return $this; in the context of observer methods - [...]

or

It's just a Magento convention to always return $this instead of void (nothing) if a method has no other return value [...]

No really. Just checked some observers in 1.9.3.x and many return nothing (void). So it's not really clear "what core code does" ;)

I've used $return $this; in my code too, but today - there will be no changes in M1 code - I'd leave it. I think - if I read others code - a void method is more clear then one with a blindly added return $this, that is never used.


Edit:

If using Aoe_Scheduler you can also return a string or array to display it in cron history.

enter image description here

Can't find any docs for this features ... related code here: https://github.com/AOEpeople/Aoe_Scheduler/blob/master/app/code/community/Aoe/Scheduler/Model/Schedule.php#L229-L259

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.