User Tools

Site Tools


php:registry

HOW TO HAVE GLOBAL VALUES USING REGISTRY SINGLETON PATTERN

Flexible and elegant way to get rid of global keyword
06 June 2008

What is registry singleton pattern?

Registry is a very usefull pattern; it allows you to store values and objects in a central place, and then retrieve them from other parts of your scripts. The registry object can be passed as an argument for functions, or as an alternative solution, can be used with another well known pattern called Singleton.

Why should I use it?

Well, if you need to access global variables along your scripts, but you don't want to use global keyword and you want a flexible and elegant solution.

PHP5 REGISTRY CLASS (W/OUT SINGLETON)

class Registry {
    private $objects = array();
 
 
    /**
    * CONSTRUCTOR
    * 
    * 
    */
    public function __construct() { }
 
 
 
    /**
    * SET AN OBJECT
    *
    * @param string $name
    * @param 
    * @return void
    */
    public function register($name, $object) {
        if ( !isset($this->objects[$name]) ) {
            $this->objects[$name] = $object;
        }
    }
 
 
    /**
    * UNSET AN OBJECT
    *
    * @param string $name
    * @param 
    * @return void
    */
    public function unregister ($name) {
        if ( isset($this->objects[$name]) ) {
            unset($this->objects[$name]);
        }
    }
 
 
    /**
    * GET AN OBJECT
    *
    * @param string $name
    * @return 
    */
    public function get($name) {
        if ( isset($this->objects[$name]) ) {
            return $this->objects[$name];
        } else return FALSE;
    }
 
} // class

PHP5 REGISTRY CLASS WITH SINGLETON

Is the same class as above, but with some small (and important) differences (private constructor, new static getInstance() method):

class Registry {
    private $objects = array();
 
    static private $thisInstance = null;
 
    /**
    * CONSTRUCTOR
    * 
    * private because we implement a singleton
    */
    private function __construct() {}
 
    private function __clone() { }      
 
    static public function getInstance() {
        if( self::$thisInstance == null)
        {
            self::$thisInstance = new Registry();
        }
        return self::$thisInstance;
    }
 
 
    /**
    * SET AN OBJECT
    *
    * @param string $name
    * @param object $object
    * @return void
    */
    public function register($name, $object) {
        if ( !isset($this->objects[$name]) ) {
            $this->objects[$name] = $object;
        }
    }
 
...
...
...
 
} // class

USAGE

For the first situation (without Singleton), we'll use it this way:

//setup the registry
$registry = new Registry();
 
// create another object (for example a database connection)
$dbhpdo = new PDO("mysql:host=localhost;dbname=database", 'root', 'pass');
 
// assign it to registry
$registry->register('dbh', $dbhpdo);
 
// now pass it as an arg to a new class (who needs global access
// you can use this in class constructor
$db = new CustomerClass($registry)
...
...
...

and somewhere in the CustomerClass we take advantage of this:

class CustomerClass() {
	private $dbhlocal;
 
	public function __construct($registry) {
		$this->dbhlocal = $registry->get('dbh');
	}
	....
}

The usage for Registry-Singleton is a little bit different, because we don't need to pass it as an argument:

//setup the registry using Singleton method
$registry = Registry::getInstance();
 
// create another object (for example a database connection)
$dbhpdo = new PDO("mysql:host=localhost;dbname=database", 'root', 'pass');
 
// assign it to registry
$registry->register('dbh', $dbhpdo);
 
// no need to pass it as an arg to a new class 
$db = new CustomerClass()
...
...
...

and somewhere in the CustomerClass we take advantage of this:

class CustomerClass() {
	private $dbhlocal;
 
	public function __construct($registry) {
		// get the registry object using Singleton
		$registry = Registry::getInstance();
		$this->dbhlocal = $registry->get('dbh');
	}
	....
}

Additional informations can be found at:
Patterns for PHP
PHPit - totally PHP

php/registry.txt · Last modified: 2013/03/16 17:40 (external edit)