Jump to content

Mysqli object in other objects


TheFilmGod

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/224022-mysqli-object-in-other-objects/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.