且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-11-30 10:22:52

呼叫

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.

当您调用Mage::registry('current_product')时,不会从数据库中重新加载产品本身—该方法所做的只是返回存储在Mage类的静态数组上的对象引用.

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.

我说前者效率较低的原因是,如果您查看registry

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;
}

您将看到Magento在返回值之前检查键是否已设置.从理论上讲,此检查的工作量更大,只需从registry抓取一次,然后重用该变量即可.

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.