Magento: Mage::registry(#39;current_product#39;) efficient?(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') 有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Magento:Mage::registry('current_product') 有效吗?


基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01