简体   繁体   English

Magento:每次循环运行时都要创建一个辅助类的新实例?

[英]Magento: creating new instances of a helper class each time a loop runs?

I created a custom helper class module which I'm calling thusly: 我创建了一个自定义助手类模块,我正在调用它:

$bench = Mage::helper('benchmark');
// ... do stuff ...
$bench->addBenchmark();
// ... more stuff ...
$bench->logResults();

This outputs the results of time benchmarks in an array. 这将输出数组中时间基准的结果。

The class is really simple, it basically just adds the current microtime() to an array then spits that array out when logResults() is called. 该类非常简单,它基本上只是将当前microtime()到数组中,然后在logResults()logResults()数组吐出。

The problem is, I have a loop where I'm calling the code above, but instead of logging a "fresh" set of benchmark figures each loop, it's compounding itself and adding to the existing benchmarks array! 问题是,我有一个循环,我正在调用上面的代码,但不是每个循环都记录一组“新的”基准数字,而是复合自身并添加到现有的基准数组!

I want it to create a new set of benchmarks each time Mage::helper('benchmark'); 我希望每次Mage::helper('benchmark');创建一组新的基准Mage::helper('benchmark'); is called. 叫做。

Any help is appreciated. 任何帮助表示赞赏。

With a helper that is not possible, unless you manually flush the content of the array upon calling the addBenchmark method. 使用无法执行的帮助程序,除非您在调用addBenchmark方法时手动刷新数组的内容。

The problem lies in the following. 问题在于以下几点。 Within Mage.php your requested helper is stored in the registry. Mage.php您请求的帮助程序存储在注册表中。 Upon next call the exact same helper object will be retrieved from the registry, instead of a new one. 在下次调用时,将从注册表中检索完全相同的帮助程序对象,而不是新的。 It only creates an instance the first time the helper is called. 它只在第一次调用帮助程序时创建一个实例。

/**
 * Retrieve helper object
 *
 * @param string $name the helper name
 * @return Mage_Core_Helper_Abstract
 */
public static function helper($name)
{
    if (strpos($name, '/') === false) {
        $name .= '/data';
    }

    $registryKey = '_helper/' . $name;
    if (!self::registry($registryKey)) {
        $helperClass = self::getConfig()->getHelperClassName($name);
        self::register($registryKey, new $helperClass);
    }
    return self::registry($registryKey);
}

You could opt for a model, since getModel() returns a new instance each time the factory method is called. 您可以选择模型,因为每次调用工厂方法时, getModel()返回一个新实例。 Though in my opinion that is a case of bad programming and also hurts you on performance. 虽然在我看来这是一个糟糕的编程案例,也会伤害你的表现。

I would opt for flushing the latest data upon making a new call to addBenchmark . 在打电话给addBenchmark时,我会选择刷新最新数据。 If you want to store all data in the helper for later processing perhaps always return the latest key in the array? 如果要将所有数据存储在帮助程序中以供以后处理,或许总是返回数组中的最新密钥? Though, thats a matter of choice and flavor, so I will leave that part up to you. 虽然这是一个选择和味道的问题,所以我会把那部分留给你。

Hopefully this helps you a bit! 希望这对你有所帮助!

Add benchmark key and make something like 添加基准键并制作类似的东西

$this->benchmarks[$key]['start'] = microtime(true);
// ... 
$this->benchmarks[$key]['finish'] = microtime(true);

so you'll have array of start-finish pairs for every iteration 因此,每次迭代都会有一系列的开始 - 结束对

Why don't you want to use Varien_Profiler? 你为什么不想使用Varien_Profiler?

Varien_Profiler::start($_profilerKey);
// ...
Varien_Profiler::stop($_profilerKey);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM