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! Link to comment https://forums.phpfreaks.com/topic/175143-static-util-classes-whos-creates-the-db-connection/ 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 Link to comment https://forums.phpfreaks.com/topic/175143-static-util-classes-whos-creates-the-db-connection/#findComment-923368 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 Link to comment https://forums.phpfreaks.com/topic/175143-static-util-classes-whos-creates-the-db-connection/#findComment-923629 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.