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

function &getInstance() {
        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.

« Previous: Function Arguments & Singletons
Next: A Request Wrapper & Conclusion »



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