Jump to content

(dis)advantages to Global Data and Registry-Objects


Goldeneye

Recommended Posts

Trying to improve my script-architecture, I got curious about other ways to pass application-data around aside from using the$GLOBALS array. The only other way I really found was to use an object called a "Registry". I just ended up being confused regarding when which method is better than the other, let alone how to even use a registry-object in my scripts. So my questions are:

- When is it better to use a registry object in place of global data?

- how would you use a registry?

- how would a registry object get passed around, through other objects and functions?

 

Heres the registry I found...

	class registry {
	var $_cache_stack = array();

	function __construct(){
		$this->_cache_stack = array(array());
	}
	function set($key, &$item){
		$this->_cache_stack[0][$key] = &$item;
	}
	function &get($key){
		return $this->_cache_stack[0][$key];
	}
	function isEntry($key){
		return ($this->getEntry($key) !== null);
	}
	function &instance(){
		static $registry = false;
		if (!$registry) $registry = new Registry();
		return $registry;
	}
	function save(){
		array_unshift($this->_cache_stack, array());
		if (!count($this->_cache_stack)) exit('Registry lost!');
	}
	function restore(){
		array_shift($this->_cache_stack);
	}
}

 

Link to comment
Share on other sites

$_GLOBALS is basically a Registry itself. Using a class just gives you more flexibility on what you can do.

 

A registry can have a purpose but a lot of the time it's misused. Basically just functions as a store of global variables. So the real question is not which method you should use but why you need to use one.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.