NYCPHP Meetup

NYPHP.org

[nycphp-talk] OO & database connections

David Sklar sklar at sklar.com
Thu Jul 15 14:53:42 EDT 2004


putamare wrote:

> Here's a best practices question: my script has several objects, say one 
> to do authorization and one or more others to display content, or 
> whatever. Most, if not all, of these objects access multiple databases, 
> one for authorization, one for content, etc.
> 
> Is it better to have object create its own private connection object (in 
> my case I'm goofing off with the new mysqli_ functions, but I'm sure 
> this would apply equally to PEAR::DB, or even an old school connection 
> string), or creating a single connection object and passing that around 
> to the other objects as needed?

My weasely answer (below), turns on how you'd like to define "better".

The performance optimization is:

Assuming the different databases are on the same server, create one 
connection and then pass that around in your program. Saves DB server 
resources, network overhead, connection start up time, etc.

The encapsulation and object re-use optimization is:

Have each object connect separately with its own credentials, allowing 
you freedom in the future to move the databases to different servers, 
tweak access control from each object, etc.


That said, unless you have immediate and large code re-use needs, my gut 
is to do the performance optimization.

David




More information about the talk mailing list