POG1 Posted May 20, 2009 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 to use global $db; 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 ) ; $this->username = "dave"; return true; } } Quote
Floydian Posted May 21, 2009 Posted May 21, 2009 Re: Class Problem That's impossible to say without knowing how you instantiate your Database class. Is there a $db variable? If so what scope is it in? Provide a few more details, and we should be able to get ya squared away quick. Quote
Isomerizer Posted May 21, 2009 Posted May 21, 2009 Re: Class Problem 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 to use global $db; 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 ) ; $this->username = "dave"; return true; } } Not 100% sure on this, but couldn't you try extending the user class to include database class... So something like this... class User extends DatabaseClass { protected $id; public function getDetails() { $this->id = (int) $_SESSION['user_id']; $query = 'SELECT `Username` FROM `' . DBPREFIX . 'users` WHERE `ID` = ' . $this->qstr ( $id ) ; $this->username = "dave"; return true; } } Quote
POG1 Posted May 21, 2009 Author Posted May 21, 2009 Re: Class Problem Don't matter now. I have figured it out, heres what i have come up with, there will obviously be more things added to this eventually. <?php class User extends db { private $userId; public function selectUser($userId) { $this->userId = $userId; return true; } public function getDetails() { $query = 'SELECT * FROM `users` WHERE `ID` = ' . parent::qstr($this->userId); if(parent::RecordCount ( $query ) == 1) { $row = parent::getRow($query); foreach($row as $key => $value) { $this->$key = $value; } } else { return FALSE; } } function isAdmin() { $result = "SELECT `Level_access` FROM `users` WHERE `ID` = " . parent::qstr($this->userId); if(parent::query($result)) { if ( parent::RecordCount ( $result ) == 1 ) { $row = parent::getRow($result); return ($row->Level_access == 1) ? TRUE : FALSE; } else { return FALSE; } } else { return FALSE; } } } ?> Quote
POG1 Posted May 22, 2009 Author Posted May 22, 2009 Re: Class Problem I am still quite new to classes and I'm stuck again :( I am trying to specify the URL to my template folder in the smarty template class. The default is just a static string and when i try to add a variable into it there is an error, here is what i tried var $template_dir = 'lib/templates/'. $TEMPLATE['url']; Quote
Floydian Posted May 23, 2009 Posted May 23, 2009 Re: Class Problem Rules regarding setting property values are quite strict. The property can be set in your constructor. Normally, dynamic values for properties would be initialized in the constructor. Hope that helps... Quote
Vali Posted May 25, 2009 Posted May 25, 2009 Re: Class Problem You declared the $db globally in your page. Just use the global keyword to reference it inside your class: global $db; or pass it to the class. Quote
POG1 Posted May 27, 2009 Author Posted May 27, 2009 Re: Class Problem I added a function that sets the value of the URLs, if anyone wants it here it is, I added inside the smarty class. function select_template($template) { if (file_exists('lib/templates/'.$template)) { $this->template_dir = 'lib/templates/'. $template; $this->compile_dir = $this->template_dir .'/templates_c'; } else { $this->template_dir = 'lib/templates/default'; $this->compile_dir = $this->template_dir .'/templates_c'; } } Quote
Zeggy Posted July 9, 2009 Posted July 9, 2009 Re: Class Problem Sorry, but you've missed the point of OOP. A User is NOT a type of database, which is why you should not extend the db class. A user doesn't connect to a database server, it doesn't execute queries. It might use the database in some functions, which is why you should try to make the two classes work together instead of making one an extension of the other. If you have another class, say a Game class or an Enemy class that also needs to use the database, and you also extend the database class, then each class will connect to the database in separate connections. In fact, if you have more than one User instance, each one will have separate connections as well, as they are all separate db child classes. What you want is for each page to use just one connection. All users, all classes should share a single connection. To get the two classes to work together, you can either set $db as a global, or pass it as a reference to the constructor or some other function: function setDb(&$db) { $this->db = $db; } function otherFunction() { //If you need to use the database object $this->db->query('query'); } Quote
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.