POG1 Posted May 20, 2009 Share Posted May 20, 2009 I have 2 separate classes, database and user class. Inside the user class I have to do a query and if i try to use the db class it produces an error, is there any way i can make this work? I have tried things but with no success. class User { protected $id; public function getDetails() { $this->id = (int) $_SESSION['user_id']; $query = 'SELECT `Username` FROM `' . DBPREFIX . 'users` WHERE `ID` = ' . $db->qstr ( $id ) ; // do stuff? return true; } } The error it says is "Fatal error: Call to a member function qstr()" Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 20, 2009 Share Posted May 20, 2009 $db is undefined. Quote Link to comment Share on other sites More sharing options...
POG1 Posted May 20, 2009 Author Share Posted May 20, 2009 db is defined sorry, i just copy and pasted the class that isn't working. How can I get db to work inside of this class? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 20, 2009 Share Posted May 20, 2009 You can pass $db as a param to the method getDetails() or pass it via the constructor when you instantiated the class. Pass as param: class User { ... public function getDetails ($db) { ... $db ... } } $db = ...; $user = new User; $user->getDetails($db); Via constructor: class User { private $db; public function __construct ($db) { $this->db = $db; } public function getDetails () { ... $this->db ... } } $db = ...; $user = new User ($db); $user->getDetails(); Does that help? Quote Link to comment Share on other sites More sharing options...
POG1 Posted May 20, 2009 Author Share Posted May 20, 2009 Thanks for the help but i have another problem now. how can i use the functions for the db? I tried this... $this->db->qstr($id) Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 20, 2009 Share Posted May 20, 2009 It's kind of odd the way you're writing it. The reason it doesn't work is because $id is undefined. You have to reference the class's $id with $this->id; however, as I said, it's an odd way to set $this->id each time. Try it this way. class User { private $db; protected $id; public function __construct ($db, $id) { $this->db = $db; $this->id = intval($id); } public function getDetails() { $query = 'SELECT `Username` FROM `' . DBPREFIX . 'users` WHERE `ID` = ' . $db->qstr ( $this->id ) ; // do stuff? return true; } } $db = ...; $id = $_SESSION['user_id']; $user = new User($db, $id); $user->getDetails(); Quote Link to comment Share on other sites More sharing options...
POG1 Posted May 20, 2009 Author Share Posted May 20, 2009 Ok it didn't work would there be a better way of getting the class working with just 1 function in it? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 20, 2009 Share Posted May 20, 2009 Oh oops. Try this class User { private $db; protected $id; public function __construct ($db, $id) { $this->db = $db; $this->id = intval($id); } public function getDetails() { $query = 'SELECT `Username` FROM `' . DBPREFIX . 'users` WHERE `ID` = ' . $this->db->qstr ( $this->id ) ; // do stuff? return true; } } $db = ...; $id = $_SESSION['user_id']; $user = new User($db, $id); $user->getDetails(); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.