valaris Posted June 20, 2008 Share Posted June 20, 2008 Hi, right now im trying to write a small API that can hopefully grow over time. Right now its functional with a class to handle all sql operations(queries and such), as well as having a few other classes for login. My question, being fairly new to OOP in general, what is the best way to use this database class in my other classes? Right now i am just creating a new instance of a db class and connecting/pumping queries out. Is there a way i can use one connection for all classes? I have about 6 classes right now to perform various unrelated tasks that all call a new database class each time they are run, is this the way i should be doing this? Thanks again. Link to comment https://forums.phpfreaks.com/topic/111167-utilizing-db-wrappers-in-other-classes/ Share on other sites More sharing options...
valaris Posted June 20, 2008 Author Share Posted June 20, 2008 Anybody here? I suppose this may be more of a design issue. Or if anybody has any code examples of a database class used in other classes. Link to comment https://forums.phpfreaks.com/topic/111167-utilizing-db-wrappers-in-other-classes/#findComment-570652 Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 Make the database class a singleton so it uses one connection throughout the whole thing. Link to comment https://forums.phpfreaks.com/topic/111167-utilizing-db-wrappers-in-other-classes/#findComment-570709 Share on other sites More sharing options...
valaris Posted June 21, 2008 Author Share Posted June 21, 2008 wow this helped immensely...thank you so much. Is this the way databases, or other classes that need 1 instance such as session classes are used in api's or cms's? Thanks. Link to comment https://forums.phpfreaks.com/topic/111167-utilizing-db-wrappers-in-other-classes/#findComment-571196 Share on other sites More sharing options...
DarkWater Posted June 21, 2008 Share Posted June 21, 2008 Basically, yes. Then you do something like: $connection = DB::getInstance(); Link to comment https://forums.phpfreaks.com/topic/111167-utilizing-db-wrappers-in-other-classes/#findComment-571212 Share on other sites More sharing options...
valaris Posted June 21, 2008 Author Share Posted June 21, 2008 Awesome. Maybe I should move this to design with all my design questions ><. Anyways i guess my next question involves implementation. I've successfully made a database singleton class, and a user singleton class. The main user class is responsible for mantaining the user object, of which other classes can derive from it (such as login). How do i encorporate my database class into my login class? Would the best way be to in the user class just make a class variable $db = mysql::getInstance(); ? Link to comment https://forums.phpfreaks.com/topic/111167-utilizing-db-wrappers-in-other-classes/#findComment-571224 Share on other sites More sharing options...
valaris Posted June 22, 2008 Author Share Posted June 22, 2008 anybody on this? Link to comment https://forums.phpfreaks.com/topic/111167-utilizing-db-wrappers-in-other-classes/#findComment-571327 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.