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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.