Magento: Mage::registry('current_product') efficient?
magento, php
Solution
Calling
Mage::registry('current_product')->getName()
over and over again will be slightly less efficient than
$temp = Mage::registry('current_product') then
$temp->getName() over and over
But it's not so bad that I'd be super concerned about. If you're setting a coding style, pick the second. If you have a bunch of old code with the former, don't worry about its performance.
The product itself won't be reloaded from the database when you call `Mage::registry('current_product')` — all this method does is return an object reference that's been stored on a static array of the `Mage` class.
The reason I say the former will be slightly less efficient is, if you take a look at the source of `registry`
#File: app/Mage.php
public static function registry($key)
{
if (isset(self::$_registry[$key])) {
return self::$_registry[$key];
}
return null;
}
You'll see Magento check if the key is set before returning a value. This check, theoretically, is more work that grabbing it from `registry` once and then reusing the variable.
However, practically speaking, you're going to have bigger bottlenecks before this is a real problem.
Problem
This is probably something obvious if you know the process behind it.. But when you use `Mage::registry('current_product')` on a product page, for example, are you merely referencing something that is already "loaded" or are you loading it every time you run that line of code? In other words, which is more efficient? (pseudocode below) ``` Mage::registry('current_product')->getName() over and over ``` Or... ``` $temp = Mage::registry('current_product') then $temp->getName() over and over ```