Aureole Posted October 14, 2007 Share Posted October 14, 2007 I have a member, render and database class. I want to be able to use the database class within the other two. Is this possible? Would I maybe do something like... class database extends member, render { I'm not sure but I need to use the database class within my other classes and yes it would extend the other classes and provide them with functionality, right now when trying to use my database class in another class I get the dreaded call to a member function on a non-object. Quote Link to comment Share on other sites More sharing options...
emehrkay Posted October 14, 2007 Share Posted October 14, 2007 no need to extend, just set a property of the class as an instance of the database class class member{ private $_db = false; public function __construct(){ $this->_db = new database(); } } now just do $this->_db->method(); Quote Link to comment Share on other sites More sharing options...
Aureole Posted October 14, 2007 Author Share Posted October 14, 2007 Ok just so I understand correctly... My member class looks kind of like this... <?php class member { var $c_time; var $update; var $setwhat; var $setequals; function last_action() { $this->update = $update; $this->setwhat = $setwhat; $this->setequals = $setequals; $this->c_time = time(); $query = "UPDATE `$this->update` SET mem_last_action='{$this->c_time}' WHERE $this->setwhat='{$this->setequals}'"; $result = mysql_query($query); } } ?> So to do it your way I would do, this: <?php class member { var $c_time; var $update; var $setwhat; var $setequals; private $database = false; public function last_action($update, $setwhat, $setequals) { $this->update = $update; $this->setwhat = $setwhat; $this->setequals = $setequals; $this->database = new database; $this->c_time = time(); $database->simpleupdate('member', 'mem_last_action', $this->c_time, $_SESSION['mem_id']); } } ?> Also I would need to instantiate database first, correct? Quote Link to comment Share on other sites More sharing options...
trq Posted October 14, 2007 Share Posted October 14, 2007 You would probably be better of instantiating the db object within the member objects constructor. This way you can easily use it within any method it is required. <?php class member { var $c_time; var $update; var $setwhat; var $setequals; function __construct() { $this->database = new database; } public function last_action($update, $setwhat, $setequals) { $this->update = $update; $this->setwhat = $setwhat; $this->setequals = $setequals; $this->c_time = time(); $this->database->simpleupdate('member', 'mem_last_action', $this->c_time, $_SESSION['mem_id']); } } ?> Its a bit hard to tell if your using php5 or 4, but Ive given an example in php5. Quote Link to comment Share on other sites More sharing options...
Aureole Posted October 14, 2007 Author Share Posted October 14, 2007 Thanks a lot I'll give it a try. I'm using PHP 4.4.7, it's in my signature. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted October 15, 2007 Share Posted October 15, 2007 I personally prefer to pass objects to classes (instead of creating new objects all over shop and the overhead that goes with it) SO $db = new database; $member = new member; $member->$db = $db; $render = new render; $render->$db = $db; then inside the render and member classes use $this->db-> Quote Link to comment Share on other sites More sharing options...
Simon Moon Posted October 22, 2007 Share Posted October 22, 2007 Or use a singleton as database object, then whenever you need the db in your class you say simply $db = Database::getInstance(); and you are using the exact same object all around, while not having to pass the db object all over the place. 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.