Forum Moderators: coopster

Message Too Old, No Replies

integrate db abstraction into late stage site

         

distorto

3:56 pm on Sep 29, 2008 (gmt 0)

10+ Year Member



I'm working on a fairly large project and it's changed quite a bit over its lifespan. Because many components were developed in a proof-of-concept way, to show clients how various features would work and then subsequently integrated straight into the production code, it's become a little messy.
I'm trying to reign in the db portion of the code right now.
In this project, each page instantiates a number of objects to build the page. Each object requires db access. I want to integrate a central db abstraction object into the code.

Right now, each class creates a db object in its constructor. I'm assuming that's a bad idea, since we wind up with 4 or 5 db connections for each page. I'd like to make one db class that escapes all the query data and forms nice queries.
But should I create the db object and then pass that object into each class? ( something like:
$useful_object = new Thing_Creator($db_abstraction_object)
)
Or is there another approach for integrating these db abstraction layers into a large code base?

grallis

5:24 pm on Sep 29, 2008 (gmt 0)

10+ Year Member



Unless you have a very, very large web app that includes tons of classes, I wouldn't be so worried about creating a new DB object every time, but if you're concerned I would use a public static methods.

A short rundown if you're unaware what a static method does is, it allows you to call a class method without creating a whole object.

You would declare the method as such -

class dbAbstraction {

public static function dbConnect(){
// code to connect here
}

}

Then you can call this method from another class like this: dbAbstraction::dbConnect();

If you already had some knowledge in this method, I hope this helps, if you need more assistance, feel free to let me know and I'll get back to this topic with a more thorough explanation