Page 1 of 1

Sharing a database connection...

Posted: Tue Feb 01, 2011 6:01 pm
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?

Re: Sharing a database connection...

Posted: Tue Feb 01, 2011 7:17 pm
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/

Re: Sharing a database connection...

Posted: Tue Feb 01, 2011 7:25 pm
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)

Re: Sharing a database connection...

Posted: Wed Feb 02, 2011 11:30 am
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!

Re: Sharing a database connection...

Posted: Wed Feb 02, 2011 11:43 am
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.