Goldeneye Posted November 22, 2010 Share Posted November 22, 2010 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); } } Quote Link to comment Share on other sites More sharing options...
requinix Posted November 22, 2010 Share Posted November 22, 2010 $_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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.