Magento:Mage :: registry('current_product')有效吗?

时间:2013-08-09 22:29:44

标签: php magento

如果你知道它背后的过程,这可能是显而易见的..但是当你在产品页面上使用Mage::registry('current_product')时,你只是引用已经“加载”的东西或者你正在加载它每次运行那行代码?

换句话说,哪个更有效率? (下面的伪代码)

Mage::registry('current_product')->getName() over and over

或者...

$temp = Mage::registry('current_product') then
$temp->getName() over and over

1 个答案:

答案 0 :(得分:17)

调用

Mage::registry('current_product')->getName()

一遍又一遍地稍微效率低于

$temp = Mage::registry('current_product') then
$temp->getName() over and over

但是我并不是那么害怕。如果您正在设置编码样式,请选择第二个。如果你有一堆旧的代码与前者,不要担心它的性能。

当您调用Mage::registry('current_product')时,不会从数据库重新加载产品本身 - 所有这些方法都会返回一个存储在Mage类的静态数组中的对象引用。

如果你看一下registry

的来源,我说前者效率会稍微低一点的原因是
#File: app/Mage.php
public static function registry($key)
{
    if (isset(self::$_registry[$key])) {
        return self::$_registry[$key];
    }
    return null;
}

你会看到Magento在返回值之前检查是否设置了键。从理论上讲,这种检查更多的是从registry抓取它然后重用变量。

然而,实际上,在这是一个真正的问题之前,你会遇到更大的瓶颈。

相关问题