Magento: Be Careful with Magic Properties

In certain places in Magento, you can simply access the data from an object by property, but beware!

For instance, in the product template the following two code snippets have the same effect :-

$_qty = $_product->stock_item->stock_qty;


$_qty = $_product->getStockItem()->getStockQty();

However only the latter will work if you call it from the category view. This is because the product is not fully loaded, and even doing a

$_newproductobject = Mage::getModel('catalog/product')->load($_product->getId());

will not help!

So the moral of this is, use the getters not the magic property.

Tags: phpmagentopropertyoomagic method