Hey all,
I'm trying to make a small CMS for fun but I'm getting caught up on some semantics with OOP. I have a basic database class and then I have an administrator class. The admin class has to make database queries, so what I've been doing is having the admin class extend the db class and then passing the link to the db resource via construct. My code looks something like this:
class admin extends mysql {
function __construct($link) {
$this->link = $link; //$this->link is in the mysql class
}
}
$db = new mysql($host,$user,$pass,$name);
$admin = new admin($db->link);
I know there should be an easier method to this without extending the db class (which I assume calls an entirely new instance of it). A friend told me to just pass the $db object to the admin class and store it there like so:
class admin {
var $db;
function __construct($db) {
$this->db = $db;
}
}
$db = new mysql($host,$user,$pass,$name);
$admin = new admin($db);
Is this the correct way to do things? Is there an easier way to reference the db link if it's already been established?
Thanks!