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); } } Link to comment https://forums.phpfreaks.com/topic/219433-disadvantages-to-global-data-and-registry-objects/ 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. Link to comment https://forums.phpfreaks.com/topic/219433-disadvantages-to-global-data-and-registry-objects/#findComment-1137848 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.