5

As I understand, helpers are singletons in Magento. I was wondering what the life span of the singleton is in Magento. Is only 1 instantiated for the life of the app (web server process start to finish) or is it per request? Or is it something else?

I have a helper that memoizes some data, and I was wondering how long it would hold on to it. I figured it would be for the life span of the helper.

2
  • 1
    What are you using to "memorize" the data?
    – mbalparda
    Commented Jun 2, 2015 at 19:16
  • It's stored in the instance of the object with $this->_variable
    – Shilpam
    Commented Jun 2, 2015 at 20:23

2 Answers 2

3

Most variables in PHP last as long as the page request or an execution lasts. So, if you instantiate a helper object for a certain page request or a script execution in command line, the helper will retain the cached $this->_variable variable for that request only. When you refresh the page or run the script again, it will re-create itself. Consider the following dummy helper.

class My_Extension_Helper_Data extends Mage_Core_Helper_Data
{
    protected $_temp = null;

    public function getTemp()
    {
        if (!isset($this->_temp)) {
            $this->_temp = rand(0,10);
        }

        return $this->_temp;
    }
}    

Call $helper->getTemp() and see what variable it returns. You'll see that it changes on every single run or page request.

1
  • Pretty much what i have said in my answer
    – Shaughn
    Commented Jun 3, 2015 at 10:31
4

As you can see below, the helper is set in registry:

/**
 * Retrieve helper object
 *
 * @param string $name the helper name
 * @return Mage_Core_Helper_Abstract
 */
public static function helper($name)
{
    $registryKey = '_helper/' . $name;
    if (!self::registry($registryKey)) {
        $helperClass = self::getConfig()->getHelperClassName($name);
        self::register($registryKey, new $helperClass);
    }
    return self::registry($registryKey);
}

which basically sets a static var. That being said and more simply put, the helper will only be instantiated and initialised for the life of the app. Once the helper is called in a new process, the data previously set will be lost.

If you are looking to store data to be reused later on, you could use core/sessions else implement a session class of your own.

2
  • Is a new process guaranteed per request or per stop/start of the webserver?
    – Shilpam
    Commented Jun 2, 2015 at 22:20
  • 2
    Php guarentees a full clean up at request shutdown. Only things that can circumvent this is extensions written in C as they can override the request destructor - this is how persistent connections to databases are maintained. Semantics: php is only a new process per request in the old days of plain CGI. These days, several requests are handled by fastCGI interface (php-fpm) or apache module (mod_php).
    – user4351
    Commented Jun 2, 2015 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.