My site uses a file called library.php, which houses various classes and functions. One such class is called "DB," which handles all of my database functions. Rather than creating a new object in every class that uses the DB, I'd like to create a global object that all classes can call upon. I've tried the following:
class Singleton {
function getInstance($class)
{
static $object = NULL;
if ($object == NULL)
{
$object = new $class;
}
return $object;
}
}
$thisPage['singleton'] = new Singleton;
$db = $thisPage['singleton']->getInstance('DB');
While $db is accessible on a local level, my classes can't seem to access it. Is what I'm trying to accomplish possible with PHP, and how can it be done?