I have created an API integration with a third-party retailer. I have created my own Mage::dispatchEvent
calls before and after the communication. I mostly use these for logging/debug mode, but I now have the opportunity to use them to manipulate data from another, loosely related module that would also use this API.
In the below example is it possible to manipulate $post_string
in this local scope based on what my observer may do?
Mage::dispatchEvent('my_api_delete_before', array('post_string'=>$post_string, 'my_cart'=>$this));
$result = $this->doApiCommunication($post_string);
Mage::dispatchEvent('my_api_delete_after', array('post_string'=>$post_string, 'result'=>$result, 'product'=>$product, 'cart_item_id'=>$_item->cartItemId));
Best Answer
If
$post_string
is a string, then no. Changes done to it in the observer class would not be shown in this scope because strings aren't passed by reference to functions.There are several solutions for that:
Quick but not recommended:
PHP does give you a workaround, by forcing the string to be passed by reference to the function by adding an
&
before the the variable name, like so:But the reason this isn't recommended, as @dedmeet said in his comment:
Directly from the PHP manual:
So here is how to do it in a way that is
Cleaner and recommended:
A better solution would be to create a
Varien_Object
because classes are always passed by reference. And any class that extendsVarien_Object
and theVarien_Object
itself, gives you the ability to use the getters and setters you find around Mangento.So to implement the
Varien_object
in the OP's example: