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 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!

« Previous: The Registry Pattern



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