I don't think declaring anything as global is good OO practice. I'm not an OO expert but I'd definitely avoid doing that.
One idea I hit on is the idea of using a factory class that will supply a PEAR DB object to each of the classes inside my "package." I create a factory class that has get and set methods for the 5 parts of the PEAR DB connecting string (hosttype, username, password, hostname, database), and then has a few methods to build those properties into a connection string and eventually return a new PEAR DB object.
Then, my factory class has a method like newCategory() (category is a class in my application). It includes the file that holds my category class, creates a new instance of it by passing a PEAR DB object to the Category constructor. Then it returns the instance of that new class.
For instance, here's what that code allows me to do in my app like.
// this should probably be an include on every page of your app since it's a useful 'global' thing to have
require("Factory.php");
$factory = new Factory();
$factory->setHosttype("pgsql");
$factory->setUsername("teds_master");
$factory->setPassword("95hotDAWG");
$factory->setHostname("66.202.79.104");
$factory->setDatabase("tedsonline_com");
// now we have an object ready make other objects
// make a new category object
$category = $factory->newCategory();
I'm new to this approach and would love some input from other users. This seems to make life a little easier but maybe I'm missing some OO concept here. Below is the code for the factory class and Category class.
<?php
class ECommerceFactory
{
// private properties
var $hosttype;
var $username;
var $password;
var $hostname;
var $database;
// null constructor
function ECommerceFactory()
{
}
// sets database properties
function setHosttype($arg)
{
$this->hosttype = $arg;
}
function setUsername($arg)
{
$this->username = $arg;
}
function setPassword($arg)
{
$this->password = $arg;
}
function setHostname($arg)
{
$this->hostname = $arg;
}
function setDatabase($arg)
{
$this->database = $arg;
}
// gets database properties
function getHosttype()
{
return $this->hosttype;
}
function getUsername()
{
return $this->username;
}
function getPassword()
{
return $this->password;
}
function getHostname()
{
return $this->hostname;
}
function getDatabase()
{
return $this->database;
}
/* returns the valid DB string */
function getDBString()
{
$string = $this->getHosttype();
$string .= "://";
$string .= $this->getUsername();
$string .= ":";
$string .= $this->getPassword();
$string .= "@";
$string .= $this->getHostname();
$string .= "/";
$string .= $this->getDatabase();
return $string;
}
function getDBObject()
{
require_once("DB.php");
return DB::connect($this->getDBString());
}
/* generates objects */
function newCategory()
{
require_once("quinlan/ecommerce/Category.php");
return new Category($this->getDBObject());
}
}
?>
<?php
class Category
{
// "public" properties
var $id;
// "private" properties
var $db_connection;
function Category($arg)
{
$this->db_connection = $arg;
}
// more methods...
}
?>