Using globals in PHP
(Page 4 out of 4)A Request Wrapper
Although our registry makes the use of the global keyword completely redundant, there's still one type of global in our script: Superglobals, such as the $_POST and $_GET variables. Although these variables are very standard and it doesn't really matter if you use them, in some cases you might want to use the registry for this as well.
A simple solution would be to write a small class that provides access to these variables, which is commonly called a request wrapper, and looks something like this:
Class Request {
var $_request = array();
function Request() {
// Get request variables
$this->_request = $_REQUEST;
}
function get($name) {
return $this->_request[$name];
}
}
?>
The above example is a really simple demonstration, and there is so much more you can do with a request wrapper (e.g. automatically filter data, provide default values, and more).
To use the request wrapper, the following code is used:
$request = new Request;
// Register object
$registry =& Registry::getInstance();
$registry->set ('request', &$request);
test();
function test() {
$registry =& Registry::getInstance();
$request =& $registry->get ('request');
// Print the 'name' querystring, normally it'd be $_GET['name']
echo htmlentities($request->get('name'));
}
?>
As you can see we no longer rely on any globals anymore, and we've completely decoupled the function from any global variables.
Conclusion
In this article I've shown you how to essentially remove globals from your script, and instead rely on properly passing objects and variables. The Registry pattern is one of my favourite design patterns, because it's so extremely flexible, and as a result prevents your script from becoming a huge mess.
One thing I recommend is passing the registry object as a function argument, instead of using it as a Singleton. Although the Singleton approach is less work, it could cause problems later, and if you pass it as a normal function argument it will be easier to understand.
If you have any questions or comments, feel free to leave them below, or join us at PHPit Forums for more PHP talk!
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.