ykarmi Posted May 27, 2008 Share Posted May 27, 2008 Hey Guys, This is a pretty newbie question but my google keyword skills did not seem to help. I am very new to OOP - I used to do everything procedural until not a long time ago. I have a database class that I use to connect and talk to my database. I have another class (user class) which is supposed to add a user to the database. I need to somehow make my user class use the db class, and trying to include it with the __construct thing did not work... This is what I tried class User{ public $error; public $db; public function __construct(){ require_once "db.class.php"; $this->db = new DB; } public function create($username, $password){ //... } } If I use 'extends' for the DB class (though it's not really extending it) will I be prevented from extending the User class? I use PHP5 and I have no idea how to do this in a way that is considered good practice. Please help. Thanks! Yuval Link to comment https://forums.phpfreaks.com/topic/107535-solved-include-class-inside-another-class/ Share on other sites More sharing options...
p2grace Posted May 27, 2008 Share Posted May 27, 2008 The class should be required on the page requiring the current class. Then it would just be $DB = new DB(); inside of the function. public $db; public function __construct(){ $this->db = new DB(); } Link to comment https://forums.phpfreaks.com/topic/107535-solved-include-class-inside-another-class/#findComment-551221 Share on other sites More sharing options...
ykarmi Posted May 27, 2008 Author Share Posted May 27, 2008 Thank you very very much! This works perfectly Link to comment https://forums.phpfreaks.com/topic/107535-solved-include-class-inside-another-class/#findComment-551230 Share on other sites More sharing options...
p2grace Posted May 27, 2008 Share Posted May 27, 2008 Glad I could help Would you mind hitting the "Topic Solved" button when you have a chance? Thank you! Link to comment https://forums.phpfreaks.com/topic/107535-solved-include-class-inside-another-class/#findComment-551233 Share on other sites More sharing options...
ykarmi Posted May 28, 2008 Author Share Posted May 28, 2008 Done Thanks again Link to comment https://forums.phpfreaks.com/topic/107535-solved-include-class-inside-another-class/#findComment-552056 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.