Make a block class a singleton

Hello guys,

I’m doing the following thing in my frontend layout

<block class="VendorModuleBlockCategoryMyCustomBlock" name="tpl1" ... /> <block class="VendorModuleBlockCategoryMyCustomBlock" name="tpl2" ... /> 

In this class I have a function doing things. I add a variable to this function for performance optimisation. As such :

if ($this->myVar === null) { [stuff...] $this->myVar = 'ok'; } return $this->myVar; 

Sadly I will enter twice in the if condition because Magento instantiates my class twice.

It is normal behaviour though.

Any idea to make my class a singleton or something like that.

Thank you in advance 😉

submitted by /u/BewareTheStareOf
[link] [comments]