Magento – Difference between $this->helper(‘catalog/image’)->… vs. (string)$this->helper(‘catalog/image’)->

ee-1.12magento-enterprise

Can someone explain how the image helper works in Magento and if the following two lines are identical? They appear to be identical strings when I run var_dump($temp), but memory usage says otherwise on certain products (outside of the scope of this question, but it happens on certain products and casting the string alleviates the problem).

$temp = $this->helper('catalog/image')->init($product, 'small_image')->resize(135);

$temp = (string)$this->helper('catalog/image')->init($product, 'small_image')->resize(135);

Best Answer

Those 2 lines are not identical.
After the first line $temp will be an instance of Mage_Catalog_Helper_Image. This is because the resize method returns the currend object.

After the second line $temp will be a string.
When casting an object to a string it's the same as calling $object->__toString();
You can take a look at the Mage_Catalog_Helper_Image::__toString() methods. This is the one that actually creates the cached, resized image if it doesn't exist and returns the url of the image.

About the memory usage, I cant say much. What are you doing with $temp after the lines above? Maybe you do something that reaches the memory limit.

Related Topic