Using globals in PHP
(Page 3 out of 4)The Registry Pattern
The best way to make certain objects available to all the components in our script is to use a central "holding" object, which holds all our objects. This holding object is officially called a Registry, and is extremely flexible, yet simple at the same time. A really simple Registry object looks like this:
Class Registry {
var $_objects = array();
function set($name, &$object) {
$this->_objects[$name] =& $object;
}
function &get($name) {
return $this->_objects[$name];
}
}
?>
The first step in using the Registry object is to register the objects, using the set() method:
$db = new DBConnection;
$settings = new Settings_XML;
$user = new User;
// Register objects
$registry =& new Registry;
$registry->set ('db', $db);
$registry->set ('settings', $settings);
$registry->set ('user', $user);
?>
Now that our Registry object holds all our objects, we only have to pass the Registry object to a function, instead of the three separate objects. See the example below:
function test(&$registry) {
$db =& $registry->get('db');
$settings =& $registry->get('settings');
$user =& $registry->get('user');
// Do something with the objects
}
?>
And what's even better is that we won't have to change anything when we add another object to our script. We just have to register the new object with the Registry, and it can immediately be used in all the components.
To make it even easier to use the registry, we can change it into a singleton. Since there should only be one registry in our script, a singleton is very suitable for this task. Add the following method to the Registry class:
static $me;
if (is_object($me) == true) {
return $me;
}
$me = new Registry;
return $me;
}
And then it can be used as a Singleton, for example:
$db = new DBConnection;
$settings = new Settings_XML;
$user = new User;
// Register objects
$registry =& Registry::getInstance();
$registry->set ('db', $db);
$registry->set ('settings', $settings);
$registry->set ('user', $user);
function test() {
$registry =& Registry::getInstance();
$db =& $registry->get('db');
$settings =& $registry->get('settings');
$user =& $registry->get('user');
// Do something with the objects
}
?>
As you can see we don't have to pass anything to the function, and we didn't have to use the global keyword either. The Registry pattern is the ideal solution for this problem, and very flexible.
June 27th, 2006 at 9:33 pm
Using globals in PHP
The headline could misslead you
PHPit.net published an article how to use GLOBALs, how to use singeltons and the registry pattern in PHP.
Nice to read and includes some hints for people having trouble understanding or who wants to know what those bu…
June 27th, 2006 at 11:19 pm
PHPit.net: Using globals in PHP
June 28th, 2006 at 8:59 pm
Why not just use Constants instead? There also various ways to register certain globals and constants to, I use:
$GLOBALS[”constants”][] array(”function” => “”, “file” => “”, “line” => “”, “name” => “”);
I can iterate over this array and find out where I place what, when, where and why, plus I could extend the array with anything I want, even if I want, wrap it in a function:
function register_constant($array_constant) {
}
I’m using techniques such as this and others on my site and in its various applications.
June 29th, 2006 at 10:56 pm
Hi
I am kind of new to php and I liked this technique, but still insted of using Class Registry Why don’t you use simple array here and that will saveral function call, and that may improve the prformance,
I mean to say we are using globals because in most cases we want to improve speed of the system and if we again create several classes and function then it may slow down the system.
Please correct me if I am wrong. I want to learn php
July 5th, 2006 at 4:12 am
[…] PHPit - Totally PHP » Using globals in PHP In this article you will be shown how to properly use globals in PHP. We will take a look at the global keyword, function arguments, Singletons and the Registry pattern. […]
July 9th, 2006 at 12:17 pm
This article is very good.
It points out some nice solutions…
i hate using globals, and now i got the idea how to avoid it…
thanks to the author…
July 12th, 2006 at 5:22 am
Very nice tutorial, I have been looking ways to improve my coding, and I will use this technique on my website.