So before going to all about details of set get and unset magento registry variable we need to discuss little about all those global variable patterns.So generally magento uses global variable by singleton pattern and registry partern. As.As our article is about registry pattern so we need to focus only on regisrty partern.

If you see Mage.php(app/Mage.php) file then you will be getting three static function on Mage class.Those three function implements the Magento’s registry logic.As we know the mage class is the magento’s main application class, so this class contains many other static helper methods and also contain run method which is what starts Magento bootstrapping and controller dispatch process.

So those three registry methods are :

public static function register($key, $value, $graceful = false)
public static function unregister($key)
public static function registry($key)

you can say in this way:

Mage::register($key, $value, $graceful = false)
So from from above function you might be understood clearly how and when we can use methods for declaring our variable globally. Still.Still lets see one by one as per our requirement for better understand.

Set to registry

Also if you see that Mage.php file then you will be getting a static $_registry class variable, which stores any object or value through register method.So lets take a look that methods how that stores in that static variable.

You can see this function which is checking if that variable is already set or not. If set then this will through an exception or gracefully return null.This concept is used in this way to avoiding the accident overwriting someone else’s global without explicitly unregistered it.
Lets see a little example:

Here if anyother piece of previously executed code ha also defined a $my_var as global this code will silently overwrite that value and of-course will cause problem on latter moment.So magento use this concept to avoid such accidental cases.

Get from registry

This method is very simple and nothing here to explain as its just take variable.

Unset the registry

Finally if you want to make your variable unavailable then use unregister method to remove from the registry.Check the below function which is present at app/Mage.php file

This methods uses the passed in key to unset the variable stored in static $_registry class variable.Also you can see in this function which uses a is_object function and __destruct method. Which means it will remove memory used by object rather than waiting for php garbage collector to do its job.

I hope you understand this article “set get and unset magento registry variable” and never forget to share to help others.

Thanks for visiting

Set get and unset magento registry variable
Tagged on:                     

Leave a Reply

Your email address will not be published.