1

I don't understand why the Varien_Event_Observer object has getters and setters for th 'name' attribute. However when using it in the method defined by my config xml, calling getName() returns null?

I would of take a guess that the unique node name you use in the config xml should be used as the observer name?

eg.

 <events>
    <catalog_product_save_after>
        <observers>
            <my_special_unique_name>
                <type>singleton</type>
                <class>my_special/catalog_product_save_after_observer</class>
                <method>myMethod</method>
            </my_special_unique_name>
        </observers>
    </catalog_product_save_after>
</events>

public function myMethod($observer) {

    //Returns null , not what I would expect to be "my_special_unique_name"
    $observer->getName();

}

Am I missing something, was there intention there and it was never added?

1 Answer 1

3

You get an empty value for the observer name because the name is not set. There is no line like $observer->setName() anywhere The observer instance is created in Mage_Core_Model_App::dispatchEvent() and the only operations done with it are the following:

//instantiate
$observer = new Varien_Event_Observer(); 

//set event details
$observer->setData(array('event'=>$event)); 

//call the needed method with the $observer as parameter
$this->_callObserverMethod($object, $method, $observer); 
//the line above is just a wrapper for $object->$method($observer);

If you inject somewhere between the time the observer is instantiated and the time the method is called this:

$observer->setName('Mickey Mouse');

You will be able to get the name of the observer in your method.

The value you were expecting to be returned is actually the value of variable $obsName in this piece of code:

foreach ($eventConfig->observers->children() as $obsName=>$obsConfig) {
    $observers[$obsName] = array(
        'type'  => (string)$obsConfig->type,
        'model' => $obsConfig->class ? (string)$obsConfig->class : $obsConfig->getClassName(),
        'method'=> (string)$obsConfig->method,
        'args'  => (array)$obsConfig->args,
    );
} 

That tag is used only to be able to have different observers for the same event (different tag values) or to be able to override some observer by using in your own module one of the existing tags for an event.

1
  • Its a shame they they didn't do it by default. Would of been useful - however thank you for pointing me to where it exists. I think it could be useful for Magento observers to have a concept of their own unique name. Thanks! Commented Sep 12, 2013 at 11:43

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.