The Problem:

  • I'm utilizing a db class like a wrapper (dbwrapper) to open and shut db connections via PHP PDO
  • I've 3 separate classes which extend this db wrapper
  • I instantiate 3 objects from stated classes and throughout their existence both make db queries
  • MySQL utilized in this situation.

Once the techniques from the 3 classes require db access both create PDO objects internally, utilizing the dbwrapper that they extended. Each object is storing its PDO object inside a member/area for reference alone.

The Question: My real question is this... is each object developing a separate link with the database, making 3 as a whole? Is each client of Apache creating just one link with the database or perhaps is each object while using db inside a php application developing a connection. (sounds inefficient!)

The Main Reason: I must know how these connections are handled.

I'm attempting to determine if it might be easier to have each class extend the dbwrapper or maybe it might be easier to initialize the dbwrapper without making a computerized connection the db, handle that after its needed. Rather I'd pass the dbwrapper to every objects constructor because they are initialized... allowing them to use that reference. If multiple db connections are happening i quickly think this is the easiest method to minimize overhead while conquering issues of object's scope and access.

Thanks ahead of time!

is each object developing a separate link with the database, making 3 as a whole?

Maybe. I'm not sure, but here's how you can discover. While your script is running, connect with MySQL via the consumer of preference (such as the command line) and problem the command SHOW PROCESSLIST; You will get a listing of active connections.

You will need to place a sleep inside your script to help keep it alive lengthy enough that you should run the procedure list command when you have instantiated and therefore are focusing on the 3 objects.

You will see each one connection or three. Then you will have the way to go.

(The solution will be different with respect to the underlying driver. Some motorists will reuse the bond when the DSN is similar.)

Rather I'd pass the dbwrapper to every objects constructor because they are initialized... allowing them to use that reference

This is actually the common practice. Database handles are prime candidates for that Singleton pattern too.