Are you just thinking more about how to reduce the amount of typing or fetching of item from the registry? If you're using PHP5 have you looked into using an inherited registry? This way the registry contents will be quickly accessible by any object that inherits from it (stick the registry somewhere toward the bottom of the hierarchy).
I was just going over this with my boss about ten mins ago so here's a demonstration
Code: Select all
<?php
error_reporting(E_ALL);
//This would hold your database instance as static, it will also contain wrappers
class db
{
static private $instance = null; //This will hold an instance of itself
private $dbConn;
private function __construct($host, $user, $pass, $name) //Note that this is private
{
$this->connect($host, $user, $pass);
$this->selectDb($name);
}
static public function getInstance($host, $user, $pass, $name)
{
if (self::$instance == null)
{
self::$instance = new db($host, $user, $pass, $name); //Only creates object once *ever*
}
return self::$instance;
}
public function connect($host, $user, $pass)
{
$this->dbConn = mysql_connect($host, $user, $pass);
}
public function selectDb($name)
{
mysql_select_db($name, $this->dbConn);
}
/*
Some other wrapper functions here
*/
}
class registry
{
protected static $handlers = array();
static public function add($name, $data)
{
if (!isset(self::$handlers[$name])) self::$handlers[$name] = $data;
}
static public function del($name)
{
if (!isset(self::$handlers[$name])) unset(self::$handlers[$name]);
}
protected function __get($name) //Overloaded getter
{
return self::$handlers[$name];
}
}
//This is your typical class, with the registry somewhere at the base of the hierarchy
class foo extends registry
{
function __construct()
{
//
}
public function reflect($obj)
{
print_r($this->$obj); //You'll see a DB object in here!
}
public function dump()
{
print_r($this);
}
}
registry::add('db', db::getInstance('localhost', 'someuser', 'pass', 'www')); //Stick a database object in the registry
$obj = new foo;
$obj->reflect('db');
/*
db Object
(
[dbConn] => Resource id #2
)
*/
?>
So from inside the object you have access to $this->db->whateverMethod() at all times.