TheFilmGod Posted January 10, 2011 Share Posted January 10, 2011 Ever since I started using OOP this past week, I have not been able to go back to the old "procedural" methods. HOwever, I seem to be stuck on creating a mysql connection that I can reuse in all of my classes. After I set up a connection to mysql using the mysqli object, I am unable to use the mysqli in other objects. $mysqli = new mysqli(.....); class new_class { function quickQuery () { $mysqli->query('some query') } } Obviously, this doesn't work, because $mysqli is not defined within that functions scope. One way, is to use global keyword. global $mysqli However, globals "are the root of all evil" and simply go against the idea of encapsulation in OOP. What's a way around this? HOWEVER: 1) I still want to use the mysqli object 2) I don't want to reference the $link of the db each time I instantiate a new class .... Maybe I'm asking too much? And I've google for the past hour or so. Singleton seems interesting but it requires the creation of a new db connection class. I want to use the mysqli object. Quote Link to comment https://forums.phpfreaks.com/topic/224022-mysqli-object-in-other-objects/ Share on other sites More sharing options...
TheFilmGod Posted January 11, 2011 Author Share Posted January 11, 2011 Ended up just passing the db connection class mysqli_extender { // Pass db connection through constructor function __construct($mysqli) { $this->mysqli = $mysqli; } } Quote Link to comment https://forums.phpfreaks.com/topic/224022-mysqli-object-in-other-objects/#findComment-1157677 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.