I have a module in which a protected variable is defined as follows:

protected $stores = array('a','b', 'c', 'd');

I want to load define this variable inside a custom block so I am trying to do this:

protected $staticBlock = Mage::getModel('cms/block')->load('custom_block')->getContent();

protected $stores = array($staticBlock);

This gives me PHP errors of course.

It really sounds like a newling question, but the real problem is that I tried to transform it into a function, but the entire code uses the variable without calling anything except the variable directly. I am really lost now and can't find a way to achieve this.

Any idea will be appreciated.



If I have understood what you want... why not just this?

protected $staticBlock = null;

And then, before using $this->staticBlock property (in class constructor, for instance, if that fits your needings)

$this->staticBlock = Mage::getModel('cms/block')->load('custom_block')->getContent();

About $this->stores property, I am not totally sure what you are asking for, or if that's just an example


If you want to avoid calling multiple times that code you can use a helper class, which is always instantiated as singleton https://magento.stackexchange.com/a/74991/3566, something like this

protected $staticBlock = null;

public function getStaticBlock()
    if (is_null($this->staticBlock)){
        $this->staticBlock = Mage::getModel('cms/block')->load('custom_block')->getContent();
    return $this->staticBlock;
  • What I was trying is to define the variable without having to redeclare it every time the module calls $this->staticBlock. But yes, I think I will have to do it this way. Thank you! – Gabriel G Apr 3 '18 at 16:43
  • Nice, feel free to accept the answer as valid if it helped – Raul Sanchez Apr 3 '18 at 17:58
  • I have completed a bit my answer – Raul Sanchez Apr 4 '18 at 7:36

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.