0

Given this code

<?php
class Vendor_Module_Helper_Data extends Mage_Core_Helper_Abstract
{

    protected $_currentCustomer = null;
    protected $_settingsHelper = null;

    public function involveCustomer($customer)
    {
        $this->_currentCustomer = $customer;
    }

    public function getCurrentCustomer()
    {
        if (!is_null($this->_currentCustomer)) {
            return $this->_currentCustomer;
        }
        return $this->_getCustomerSession()->getCustomer();
    }

    public function getSettingsHelper()
    {
        if (is_null($this->_settingsHelper)) {
            $this->_settingsHelper = Mage::helper('vendor_module/settings');
        }
        return $this->_settingsHelper;
    }
}

class Vendor_Module_Helper_Settings extends Vendor_Module_Helper_Data
{
    public function someMethod()
    {         
        print_r($this->getCurrentCustomer()->getData());
        exit;
    }
}

If I call

$helper = Mage::helper('vendor_module');

I would expect, as a singleton class, $helper would remain the same instance during the rest of the code flow, so...

$helper->involveCustomer($customer);

Now $_currentCustomer has been set, and it contains a customer object

But then, calling

$helper = Mage::helper('vendor_module');
$helper->involveCustomer($customer);
$settings = $helper->getSettingsHelper();
$settings->someMethod();

The customer object printed is the customer logged in, not the one involved 2 lines before... Am I missing something?

If helpers are called as singleton I bet I should get the right customer object... In fact, I have been assuming this since I started to work with Magento, and now I feel really confused :S

1 Answer 1

2

Well that is just not how Class Inheritance works

If you have a SingletonA class and extend another Singleton from your class means:

SingletonB extends SingletonA

An object SingletonB won't share the values which were set in an object of SingletonA. So to solve this one, you may want to use 'vendor_module' from inside the Vendor_Module_Helper_Settings class, instead the other way around.

<?php
class SingletonA
{
  protected $var = 'initial';

  public function set($a)
  {
        $this->var = $a;
  }

  public function get()
  {
    return $this->var;
  }
}

class SingletonB extends SingletonA
{
  public function getVar()
  {
    return $this->get();
  }
}

$singletonA = new SingletonA();
$singletonB = new SingletonB();

$singletonA->set('test');
echo $singletonA->get();
echo '---';
echo $singletonB->getVar();

outputs:

test---initial

If you take a look at the helper function of the Mage.php class of Magento1 you will ensure pretty fast that helper classes are Singletons in Magento1. Like most other object, created by Mage.php, also the helper objects are stored inside the Mage.php registry.


1
  • Oh my... Thanks Commented Oct 23, 2019 at 8:12

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.