1

Fairly new to objected oriented programming and very new to Magento programming, but trying to learn the "right way".

I'm writing a before save observer to update product attributes based on other product attributes. I've got that working (in the most basic sense). Now I'm trying to figure the right way to make it do what I want. I have...

class Company_Namespace_Model_Observer
{
    public function UpdateStuff(Varien_Event_Observer $observer)
    {
        $product = $observer->getEvent()->getProduct();
    $product->setData('MyAttribuite', 'NewData');
    }
}

That works great. But, now I want 'NewData' to be a bunch of code. My first thought was to add more public functions to the class, but that didn't work. If that was my only question, I'm sure I could find the answer already answered somewhere. But then thinking of all the examples I've seen, is inside this class the right place to put these functions, or should I have another file under a Helper directory?

Second part of the question, should the UpdateStuff() function get return values from other functions like...

$MyNewData = SomeFunction($product);
$product->setData('MyAttribuite', $MyNewData);

or should I just call SomeFunction($product) and have it set the new data?

or does it not really matter, and I'm just reading too much into what I've read about writing your code the "Magento way"

  • @Marius. Thank you for editing my post. When I saw how it displayed, I had just gone back to edit it when it told me you beat me to it. – lv2fly Nov 11 '15 at 10:47
  • Just a heads up, if you add another function within the class like private function GiveMeSomething() {} then to call it you need to use the $this object like $this->GiveMeSomething(); – Invoke Nov 11 '15 at 11:06
  • @Invoke. Thank you very much. That's something I knew, but old habits are hard to break and would have beat my head against the wall before calling myself an idiot at some point. – lv2fly Nov 12 '15 at 3:24
1

In the example you gave, if the code you are implementing is specific to your Observer class, then keep it in. Don't make your function public if you don't need to, as I don't think you'll need to access it directly from outside the class.

Not a big fan of helpers myself, developers tend to use it for the wrong reasons (group bunch of methods that aren't related to each other)

Don't only think Magento, but think from a developer perspective (Magento isn't perfect anyway). Make your code reusable, clean and optimized

1

is inside this class the right place to put these functions, or should I have another file under a Helper directory?

If you need a few lines of code all related to the current product you can write them directly in the observer function.

If there are many lines of code you can wrap them into a function for readability issue

As good programmer best practice if the calculate have to be reused somewhere else and it related to your module you can add a function in your module's helper.

if is not related to your module that maybe they can be written in a static class

...

  • you both had very good answers, only voted for his because it was first. I can't upvote anything until I hit 15 points, but as soon as I do, I found both your answers helpful. Thanks again. – lv2fly Nov 12 '15 at 3:26

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.