I would like to be able to hide my database connection from print_r so I am using a static variable. I have a base class and a few object classes. Ideally they would all share the same database connection. What is the best way of sharing this? The way I have it set up now "works" but it just doesnt feel right. Must be a better way of doing this. (logically the classes shouldnt inherit one another)
class base {
private static $db;
function __construct() {
self::$db = new DB(); // our database class
$foo = new Foo( self::$db ); // some other class that needs the same connection
}
}
class Foo {
private static $db;
function __construct( $db ) {
self::$db = $db;
}
}
See Question&Answers more detail:os