ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

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.

Next: Function Arguments & Singletons »



7 Responses to “Using globals in PHP”

  1. Casa's little Blog Says:

    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…

  2. PHPDeveloper.org Says:

    PHPit.net: Using globals in PHP

  3. Project D.O Says:

    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.

  4. Gurudutt Verma Says:

    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

  5. DevBeta: Shawn Sandy on design, development and technology » Blog Archive » PHPit - Totally PHP » Using globals in PHP Says:

    […] 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. […]

  6. Spavkov Says:

    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…

  7. Eric Lin Says:

    Very nice tutorial, I have been looking ways to improve my coding, and I will use this technique on my website.

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & The Global Keyword
  2. Function Arguments & Singletons
  3. The Registry Pattern
  4. A Request Wrapper & Conclusion
Bookmark Article
Download Article
PDF
Download this article as a PDF file