DB Object passing between classes singleton, static or other?
- by Stephen
So I'm designing a reporting system at work it's my first project written OOP and I'm stuck on the design choice for the DB class.
Obviously I only want to create one instance of the DB class per-session/user and then pass it to each of the classes that need it. What I don't know it what's best practice for implementing this. Currently I have code like the following:-
class db
{
private $user = 'USER';
private $pass = 'PASS';
private $tables = array( 'user','report', 'etc...');
function __construct(){
//SET UP CONNECTION AND TABLES
}
};
class report{
function __construct ($params = array(), $db, $user)
{
//Error checking/handling trimed
//$db is the database object we created
$this->db = $db;
//$this->user is the user object for the logged in user
$this->user = $user;
$this->reportCreate();
}
public function setPermission($permissionId = 1)
{
//Note the $this->db is this the best practise solution?
$this->db->permission->find($permissionId)
//Note the $this->user is this the best practise solution?
$this->user->checkPermission(1)
$data=array();
$this->db->reportpermission->insert($data)
}
};//end report
I've been reading about using static classes and have just come across Singletons (though these appear to be passé already?) so what's current best practice for doing this?