Magento 2 – Understanding Registry

magento2registerregistry

I know how to register, how to get the value from registry and how to unregister.

for register

$this->_coreRegistry->register('customer_save_observer_executed_' . $customer_Data->getId(),true);

for get the value from register

$this->_coreRegistry->registry('customer_save_observer_executed_' . $customer_Data->getId())

for unregister

$this->_coreRegistry->unregister('customer_save_observer_executed_' . $customer_Data->getId());

Now I don't know how long register will hold this information untill if I unregister.

Expecting valuable suggestions/help?

Best Answer

If you go to Magento\Framework\Registry you can see a __destruct method specified there.

Register basically stores your data inside an array type object using key value pair.

As soon as you stop using that class it will automatically calls the __destruct method and that will unregister that variable.

Check this link to understand how it works.

And this is when destructor wont be called.

EDIT

Lets suppose you are using the registry class inside a controller. You can access the registry throughout the controller because you have instantiated that registry class in your constructor. But as soon as your execution goes to diff controller you cant access that registry variable. Because that would be destroyed by __destruct() automatically. You dont have to unregister it.

Related Topic