Jump to content

mysqli object, classes, OOP options vs. old way


ghostcoder

Recommended Posts

Hello,

 

I have a question for you regarding mysql connections and my php application.

 

My current application uses old school database access and queries. The thing I've loved about it, is that I can easily run my mysql_query() statement no matter where I am, be it in inline code, functions and even classes. It's nice not having to worry about the database object.

 

Ok, so, I decided to start rewriting my core application, and I decided to use a myqli object. This is all great and pretty, but now I have to get the datbase object nicely into functions and classes before I can run my queries. I first simply created a new database object, and then passed it in the function call as a variable or set it as a variable when creating a new class object. I also tried putting it into a session variable, and then accessing it via that in functions and classes (which works). Ok all of these solutions work, but I get the feeling they aren't the best method. So, the best method is a Singleton class. I kind of understand that after studying it for a couple of days, but it still seems messy and it will require more code each time I need to call on the database.

 

I have a very large application, and it queries the database often. It's very lean and very fast right now using the old school mysql_query method. But it's not the 'modern' mysqli object way. So I'm trying to decide if it's worth rewriting most of my code to use a database object.

 

Is there something wrong with putting a mysqli database object into a session variable?

 

What is wrong with passing a simple variable to each function or class?

 

Both of these methods seem to work, but they still aren't as clean and simple as just using the old school method. Especially if I'm only ever using one database.

 

If you have any thoughts or feedback, I'd appreciate hearing from you.

 

Thank you.

Link to comment
Share on other sites

So, the best method is a Singleton class. I kind of understand that after studying it for a couple of days, but it still seems messy and it will require more code each time I need to call on the database.

 

The singleton is not the best method. See this post for my comments on that.

 

Is there something wrong with putting a mysqli database object into a session variable?

 

Yeah, you're exploiting the fact that sessions are superglobals. Your connection is not supposed to persist across requests like that, and I don't even think it's possible. It's a bad idea because you make it a global object.

 

What is wrong with passing a simple variable to each function or class?

 

Nothing. That is the best option.

 

Both of these methods seem to work, but they still aren't as clean and simple as just using the old school method. Especially if I'm only ever using one database.

 

Quite the contrary in fact. The method you used to use is less clean than passing an object around. You have no control over the resource in that way.

Link to comment
Share on other sites

Thank you very much Daniel. Your reply is much appreciated.

 

Ok so, I'm glad I don't need to mess with the Singleton class. Your feedback on the Session variable makes sense, so I'll avoid that also.

 

It would be cool if the PHP team had some special variable that would allow you to access specific database mysqli objects anywhere, without having to pass them around.

 

Ok so, here's my plan.

 

@ $db = new mysqli($DBhost, $DBuser, $DBpass, $DBname);

if(mysqli_connect_error()){echo "Could not connect. Please try again later."; exit;}

function myFunction($db){
    // do my query 
    $db->query("SELECT * FROM...") or die("Problem... ".$db->error);
    //do whatever here, return things if necessary
}

//call my stand alone function - pass the database object
myFunction($db);


//example of using it in a class
class myClass
{
   function  anotherFunction(){
   
      $this->data->query("SELECT * FROM.. or whatever"); //or error 
   }

}

$simpleclass = &new myClass;
$simpleclass->data = $db;
$simpleclass->anotherFunction();

 

I'm using this method now and it works a treat. I'm not saying it's perfect, but it works. Is there any reason NOT to do it this way?

Link to comment
Share on other sites

It would be cool if the PHP team had some special variable that would allow you to access specific database mysqli objects anywhere, without having to pass them around.

 

Well, they do. There is a keyword called global that can be used to make a variable global in the scope in which it's used, and there is a superglobal called $GLOBALS that contains all such variables as well. It's bad practice though, and you shouldn't do it. I don't even understand why they put it in the language in the first place. It doesn't do anything but promote bad practice. It will inevitably turn out to be different to maintain, and it will be difficult to keep track of it and debug it.

 

I've had to work on code where mostly everything resided in the global namespace, and it was nothing short of a pain in the ass.

 

Your best option is to pass the object by argument. If you need it throughout an entire class it might be worth passing it to the constructor, otherwise it would also be best to just pass it specifically to that method. Objects are passed by reference, so it won't get duplicated like scalar values and arrays will. It'll only be using 4 byte more memory (the size 32-bit integer) for a pointer to the memory address of the object, so it will by no means be an issue.

 

While we're talking about best practices, I couldn't help but notice you used or die() in the code snippet you provided. Do have a look over this blog post I wrote about that.

Link to comment
Share on other sites

Thanks again for your reply Daniel. That's good to know about the Globals. I've been programming PHP for a few years now and I've never bothered to use them. I've always used SESSION and local variables.

 

Oh, as for the die(), I only use that when testing a script. Normally I use a custom function called sql_error(). In it I pass a note along with the actual error code. So it looks like this:

 


$db->query("SELECT or other query'") or sql_error("my note about query",$db->error);

 

Then in my function I'll deal with the error, logging it and emailing a note to myself about the failure. In some cases I'll also redirect a user to a custom error page.

 

Having said that, I'll go read your blog post about the die().

 

Thanks again.

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.