You need to either pass the $ database to your custom constructor, or use one singleton, for example MrJ.
Passing a database object as a parameter is quite simple, as you already know, but you make it clear to others:
private $myDatabase = NULL; public function __construct(Database $myDatabase){ $this->myDatabase = $myDatabase; }
And then in the class anywhere you can use your database using
$this->myDatabase->select(...);
Creating an object now becomes as simple as:
$myUser = new User($database);
Obviously adjust it as needed :)
Singlets are also easy to use; create a static method inside your database class:
private static $staticInstance = NULL; public static function getInstance(){ if(Database::$staticInstance == NULL){ Database::$staticInstance = new Database(); Database::$staticInstance->connect(); } return Database::$staticInstance; }
This creates a function that will automatically create your instance of your unique database class as soon as you need it, and return that single instance when you call getInstance again.
To use this inside your other classes, simply call:
$mydb = Database::getInstance();
And then use your database object ...
Singletones are really useful in many such situations.