flash gordon Posted September 22, 2009 Share Posted September 22, 2009 I'm programming in an OO mannor. I have several util, factory, and DAO classes. My question is, who should be creating the database connection? Should the connection get aggregated into the static method or should the method create its own connection. Consider the following example: class TemplateUtil { // Method creates its own connection and logic is fully encapsulated public static function convertSiteNameToID($name) { $name = strtoupper(trim($name)); $conn = ADOConnectionUtil::createDefaultConnection("flashfac_templates"); $sql = "SELECT id FROM templates WHERE name = '$name'"; $row = $conn->GetRow($sql); $id = $row["id"]; return $id; } // Method needs a connection passed into it and relies on the context to // do so....perhaps this makes less connections and more control who connects when. public static function convertSiteNameToID($conn, $name) { $name = strtoupper(trim($name)); $sql = "SELECT id FROM templates WHERE name = '$name'"; $row = $conn->GetRow($sql); $id = $row["id"]; return $id; } } I don't have enough experience with php to know what is the better of the options. Any ideas? Thanks guys! Quote Link to comment Share on other sites More sharing options...
Highlander Posted September 23, 2009 Share Posted September 23, 2009 I would recommend the second one in your example since the first one adds a dependency that the Util class needs to know about the database when you write test-cases Quote Link to comment Share on other sites More sharing options...
flash gordon Posted September 23, 2009 Author Share Posted September 23, 2009 Thanks for the input. I'm kind of leaning that way too because if the DB user and pass changes, I'd have to go into all the utils and change the request for the connection as well, which I don't want to do. cheers Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.