Magento:Mage :: registry('current_product')有效吗? [英] Magento: Mage::registry('current_product') efficient?

查看:67
本文介绍了Magento:Mage :: registry('current_product')有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您知道其背后的过程,那么这可能很明显..但是,例如,当在产品页面上使用Mage::registry('current_product')时,您是仅引用已经加载"的内容,还是每次都加载它您运行那行代码?

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

或者...

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

推荐答案

呼叫

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.

这篇关于Magento:Mage :: registry('current_product')有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆