popcornplya Posted March 12, 2009 Share Posted March 12, 2009 If you had a database connect class, what would be the best way to inherit it in another class? Quote Link to comment https://forums.phpfreaks.com/topic/149173-solved-classes/ Share on other sites More sharing options...
corbin Posted March 12, 2009 Share Posted March 12, 2009 What do you mean by inherit it to another class? Do you mean use it? Pass it? If you literally mean you want another class to inherit it, you would just use extends. Quote Link to comment https://forums.phpfreaks.com/topic/149173-solved-classes/#findComment-783318 Share on other sites More sharing options...
.josh Posted March 12, 2009 Share Posted March 12, 2009 are you wanting this new class to be an extension/addon to the database class or are you just wanting to use a database object inside some other class? Quote Link to comment https://forums.phpfreaks.com/topic/149173-solved-classes/#findComment-783320 Share on other sites More sharing options...
popcornplya Posted March 13, 2009 Author Share Posted March 13, 2009 I want the db class to work in multiple classes, sorry for slow reply.. Quote Link to comment https://forums.phpfreaks.com/topic/149173-solved-classes/#findComment-783447 Share on other sites More sharing options...
trq Posted March 13, 2009 Share Posted March 13, 2009 Then its not extending the classes, its just simply used within them. eg; <?php class foo { private $db; function __construct() { require_once 'yourdbclass.class.php'; $this->db = new yourdbclass; } function usedbclass() { $sql = "SELECT * FROM tbl"; return $this->db->query($sql); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/149173-solved-classes/#findComment-783458 Share on other sites More sharing options...
.josh Posted March 13, 2009 Share Posted March 13, 2009 well, what do you mean by wanting it to "work?" You are asking for the best way for one class to "inherit" another class. You need to be a lot more specific with your scenario than how you're being now, if you want any meaningful answers. Depending on what you are wanting to do, you may just need to instantiate an object of your database class, and use that in your new class. Or your situation may call for extending the database class. Or you may end up needing to bust out with an interface. Who knows? Without details, we cannot give you any good advice. Quote Link to comment https://forums.phpfreaks.com/topic/149173-solved-classes/#findComment-783460 Share on other sites More sharing options...
popcornplya Posted March 13, 2009 Author Share Posted March 13, 2009 Sorry, Crayon Violent, next time i'll be more specific. And thanks thorpe, that answers my question. Quote Link to comment https://forums.phpfreaks.com/topic/149173-solved-classes/#findComment-783483 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.