Sharing a database connection...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dafydd
Forum Newbie
Posts: 3
Joined: Tue Feb 01, 2011 5:01 pm
Location: Puget Sound, WA

Sharing a database connection...

Post by dafydd »

G'morning!

This is more a "good coding practice" question, as opposed to a "how do I solve this" question. My general PHP skills are somewhere between beginner and intermediate, and I'm starting on OO PHP.

Suppose I have a database connection class:

Code: Select all

<?php

class database {}

?>
All this does is make the connection to the database and automate add/update/delete/select statements.

Say I want to manipulate some tables via table-specific classes. According to a beginner's OO PHP tutorial I found somewhere, I'd just extend the database class.

Code: Select all

<?php

class database {
  public function connect () {}
}
class tableA extends database {}
class tableB extends database {}

$useA = new tableA ();
$useB = new tableB ();

$useA->connect();
$useB->connect();

?>
(Stupid Assumption Check: Am I okay, so far? I suspect my samples are using general PHP practice, not OO-specific practice.)

If I'm understanding this correctly, $useA and $useB create their own connections to the database. This is fine if I have connections to spare. If, on the other hand, I'm sharing a database with several other departments, all of whom have their own coding volunteers with their own ideas of good practice, I might only get a couple connections before the database starts refusing...

So, suppose I only get one connection per execution/web page access? Aside from using disconnect() every chance I get, what is the best practice for having the two classes share a connection? Can I feed the connection object as an argument when I create the instances of the table classes? Something like this:

Code: Select all

<?php

class database{}

class tableA {
  private $cxn;
  function __construct ($db) {
     $this->cxn = $db;
  }
}
class tableB {
  private $cxn;
  function __construct ($db) {
     $this->cxn = $db;
  }
}

$db = new database();
$useA = new tableA ($db);
$useB = new tableB ($db);
That looks really ugly, but I can't think of another way to have a single database connection accessible to more than one other class. What better ideas are out there?

Thanks!
dafydd

--
The word "instantiate" bugs me. "Create an instance of" is too hard to type?
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Sharing a database connection...

Post by flying_circus »

What you're looking for is called the singleton design pattern and it works well for database connections.

Rather than give an example, I'll link you to a good resouce, I think it'll be helpful to you.

http://www.phpdesignpatterns.com/design ... singleton/
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Sharing a database connection...

Post by Eran »

When creating a database connection in PHP, an existing connection will be reused if you try to create the new connection with the same parameters. So you don't have to worry about that (you can force it to create a new connection though)
dafydd
Forum Newbie
Posts: 3
Joined: Tue Feb 01, 2011 5:01 pm
Location: Puget Sound, WA

Re: Sharing a database connection...

Post by dafydd »

flying_circus wrote:What you're looking for is called the singleton design pattern and it works well for database connections.

Rather than give an example, I'll link you to a good resouce, I think it'll be helpful to you.

http://www.phpdesignpatterns.com/design ... singleton/
Thanks, flying_circus. I usually note "links to documentation are great," but I forgot to say that this time.
Eran wrote:When creating a database connection in PHP, an existing connection will be reused if you try to create the new connection with the same parameters. So you don't have to worry about that (you can force it to create a new connection though)
Cool, that simplifies things. Just for my reference, do you know of a doc or web page out that describes how to force multiple connections. The idea makes me twitch, but knowledge is never bad. Thanks!
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Sharing a database connection...

Post by Eran »

do you know of a doc or web page out that describes how to force multiple connections
Look in the PHP manual for any database connection function, the last parameter is usually to force a new connection.
Post Reply