I generally choose options 4 and 5. If you write a sane autoloader, you should be able to either grab a singleton from an 'important' class or use a static function from one.
The choice between using a singleton instance or using static functions depends on the object. Database objects typically require a connection, so it's best to use a singleton (so you can use the same connection link with multiple calls)
For objects that just set/retrieve data, say .. a Session class, it may be best to use static functions (but mileage can easily vary here).
Either way, I would avoid globals if possible. Most of the time an autoloader can negate the need for them.
In reply to:
Can you expand on your use of the autoloader?
In a php file that is called on each request, define a simple autoloader using SPL or older syntax:
function __autoload($class) {
$file = 'lib/' . $class . '.class.php';
if (file_exists($file)) {
require($file);
}
}
So, in a lib
folder, you could define your DB class in a file called DB.class.php
Then, in your code, if you called:
$db = DB::getInstance();
The autoloader would automatically require()
the DB.class.php
file.
If you're not familiar with autoloaders, you can read about them here