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

时间:2023-01-20
本文介绍了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()

一遍又一遍地稍微

$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') 有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何在 magento 中获取当前产品的类别名称(在产品 下一篇:如何在 Magento 中以编程方式更新属性选项?

相关文章

最新文章