Using a mysqli connection in a class

I have a controlling script and from there I instantiate a new class object and then call a method in the class. I create a db connection before calling the method so there is an active connection in the controlling script.

I want to access it from within the class method and have tried using

global $link;

inside the class and I thought that was working but now it seems to be having problems. Is that the correct way to do that or do I need to create a new connection inside the method? The method is declared as a public function.

Thanks

  1. yes, if you extend a class you gain all of it’s protected or public methods.
  2. yes, you should keep it in one place (not in a session). You should have a config file of some sort, how that’s handled is dependent on what else you have, but essentially: read from one single place, pass this info around or make it available to scripts that need it.
  3. that is a good way to do it, but you can also check out http://php.net/manual/en/language.oop5.autoload.php for an even easier way.

I am currently using procedural, which while on the subject, is there any performance or other meaningful differences between procedural or OO with MySQLi?

And I am declaring it before the class begins. It works on another class I am using so I’m not sure what’s going on with this one. If I create the link in the class method, it works fine.

Thanks for the input. At this point I have it all working and will take your suggestions and work them in to improve it going forward. Again, I really appreciate the help.

Any kind of database connectivity sharing is best by extending the database class in other related classes who really need the database connection. Doing that you can easily access the properties of parent class. But consider the access/scope of the properties.

It shouldn’t matter where you declare $link as long as its before the class begins, one thing i do want to ask is if your using MySQLi procedural or the class method?

Personally i find OO MySQLi more efficient for speed and execution but procedural for it been easier to remember been 99% the same as MySQL. One thing you could do is using OO MySQLi and extend it onto the class and build it which you can see how to do here http://www.php.net/manual/en/mysqli.connect.php

  1. I did some reading trying to grasp this a little better and I’m getting closer. So for my class where I am going to need db access, I should extend that class with mysqli as the parent. Create the construct and when I instantiate a new version of my class, I will pass the $host, $user, $pass, $db variables and at that point I will have access to all the mysqli methods and properties using notation like $this->bind_param or $this->prepare in my class functions, is that correct?

  2. Currently I have the procedural connection to create link in an include file so anytime I need a connection, I include the file. So my connection info is only kept in one place. Do you generally keep the connection info in session variables if you are doing it by extending the class, because every time I instanitiate a new class, I need to pass that information, correct?

  3. Also, I keep my classes in a separate directory with each class in a separate file, so when I want to use one in a script, I include the class file first and then create a new instance of that class. Is that the correct way to do that or is there a more efficient way?

Again, thanks for the help and the patience. I do appreciate it.