Using globals in PHP
(Page 1 out of 4)Abstract
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.
Introduction
Whenever you're developing a new large-scale PHP script, you're bound to use global variables, since some data needs to be used by multiple parts of your script. Good examples of global data are script settings, database connections, user credentials and more. There are many ways of making this data global, but the most commonly used way is to use the global keyword, which we will explore later on in this article.
The only problem with the global keyword, and any global data for that matter, is that's it's actually a very bad way of programming, and usually tends to lead to bigger problems later on. The reason for this is that global data ties all the separate parts of your script together, and often if you make a change in one part, something else will break in another part. If you have more than a few global variables, you're whole script will eventually become a big kludge of unmaintainable code.
That's why this article will show you how to prevent this by using various techniques and design patterns. But first, let's have a look at the global keyword and how it works.
Using globals and the global keyword
By default PHP defines a few variables called Superglobals which are automatically global, and can be accessed anywhere, like the $_GET or $_REQUEST variables. These are mainly used to retrieve form data or other outside data, and there's no real harm in using these variables, since they should never be written to anyway.
But you can also use your own global variables, with the global keyword which is used to import variables from the global scope into the local scope of a function. If you don't know what I mean by "scope", have a look at the PHP Variable Scope documentation.
The following example demonstrates use of the global keyword:
$my_var = 'Hello World';
test_global();
function test_global() {
// Now in local scope
// the $my_var variable doesn't exist
// Produces error: "Undefined variable: my_var"
echo $my_var;
// Now let's important the variable
global $my_var;
// Works:
echo $my_var;
}
?>
As you can see in the above example, the global keyword is used to important variables from the global scope. Seems to work fine, and it's nice and simple, so why should you worry about using the global keyword?
There are three good reasons:
1. Reusing parts of the script is impossible
If a certain function relies on global variables, it becomes almost impossible to use that function in a different context. Another problem is that you can't take that function, and use it in another script.
2. Solving bugs is much harder
Tracking a global variable is much harder than a non-global variable. A global variable could be declared in some obscure include file, which could take hours to find, although a good text editor / IDE could help with this.
3. Understanding the code in a year will be much more difficult
Globals make it difficult to see where a variable is coming from and what it does. You might know about every global during development, but after a year or so you'll probably have forgotten at least half of them, and then you'll be kicking yourself for using so many globals.
So if we shouldn't use globals, what can we use? Let's have a look at some solutions.
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.