Using a global object will work, but there are some problems with that approach. To start with, there are reasons why globals are discouraged. Also, you need to know the name the global class has inside the other class.
The best solution is to pass a reference to the data object to any classes you want to use it in. This is done with the reference operator (&). (In PHP5, this will be done automatically.)
class mySqlObject
{
function connect($host, $user, $pass)
{
/* Code to connect to a database */
}
}
class foo
{
var $dbObj;
// Pass a reference (&) to a mySqlObject to the constructor
function foo(&$dbObj) // Constructor
{
$this->dbObj = $dbObj;
}
// Now this class can work directly on that object
function dbConnect($host, $user, $pass)
{
$this->dbObj->connect($host, $user, $pass);
}
}
/* Now you can do this */
$db = new mySqlObject();
$bar = new foo($db);
$bar->dbConnect($host, $user, $pass);
You can even access methods of the inner class like this:
$bar->dbObj->connect($host, $user, $pass);
The problem with that is that you have to know what the inner class is called (as in the global example).
Finally, since the inner and outer db object are the same object (not a copy), you could call the db object's methods directly:
$db->connect($host, $user, $pass);
and the result will be accessible to $bar through the inner object.