I think the title is right but please correct me if It is mis-leading.
The problem: I have a class which wants to use the DB class, now instead of having to "global $db;" in every method I wish to use the DB object I want to be able to place the object reference in my class properties.
Still following? OK here goes:
class user { private $id = 0; private $name = NULL; private $password = NULL; private $db; function __construct() { $this->load_db(); } private function load_db() { global $db; $this->$db =& $db; } I get an error "Object of class db could not be converted to string" which is annoying as I can't figure out how to set the var type in PHP...
Now my question is two fold:
1) How do I fix this. or 2) Is there a better way of doing it as this feels really "kack-handed".
Thanks in advance,
Dorjan
edit: Just to make sure I'm clear I do not want to make multiple instances of the same DB object. At least I believe this to be a good practice ^,^
global $dbaltogether you might be interested in en.wikipedia.org/wiki/Dependency_injection Can be as simple aspublic function setDb(PDO $dbObject) { $this->db = db; }