Tim Fletcher Posted June 15, 2008 Share Posted June 15, 2008 Hi, If you're creating a database abstraction class, is there any reason not to connect to the database in the constructor? I continually see examples of methods (like query($sql)) within the abstraction class checking to see if there's a database connection and creating it if it doesn't exist before they continue. Putting the connection into the constructor means you never need to check for the connection because if it wasn't there the object wouldn't have been instantiated from the class in the first place. I was wondering if there's some reason not to connect to the database until the very second you need it. So basically this: class Database { protected $db; public $dbname; public $host; public $user; public $password; public function __construct($host, $user, $password, $dbname) { $this->host = $host; $this->user = $user; $this->password = $password; $this->dbname = $dbname; $this->db = $this->connect(); // Any reason not to do this here? } public function connect() { $this->db = new mysqli($this->host, $this->user, $this->password, $this->dbname); return $this->db; } } Instead of this: class Database { protected $db; public $dbname; public $host; public $user; public $password; public function __construct($host, $user, $password, $dbname) { $this->host = $host; $this->user = $user; $this->password = $password; $this->dbname = $dbname; $this->db; } public function connect() { $this->db = new mysqli($this->host, $this->user, $this->password, $this->dbname); return $this->db; } public function query($sql) { // Connect to database if not already connected if( ! is_resource($this->db)) $this->connect(); // Rest of method.... } } Thanks! Link to comment https://forums.phpfreaks.com/topic/110333-connecting-to-the-database-in-the-constructor-or-as-a-method/ Share on other sites More sharing options...
corbin Posted June 15, 2008 Share Posted June 15, 2008 The first example I think of when I think of on the fly database connecting is Zend_Db. (Zend Framework's DB class....) The Zend_Db docs say: 10.1.1.5. Managing Lazy Connections Creating an instance of an Adapter class does not immediately connect to the RDBMS server. The Adapter saves the connection parameters, and makes the actual connection on demand, the first time you need to execute a query. This ensures that creating an Adapter object is quick and inexpensive. You can create an instance of an Adapter even if you are not certain that you need to run any database queries during the current request your application is serving. It makes sense to me.... If you don't automatically connect to the DB, processing/time is saved since the connection would be made if it's not used. (But, it still takes effort to initiate the class to begin with and set the settings....) The only downside I see is handling errors could be more difficult.... For slightly less coding, you could do: public function connect() { if(is_resource($this->db)) return; $this->db = new mysqli($this->host, $this->user, $this->password, $this->dbname); return $this->db; } public function query($sql) { // Connect to database if not already connected $this->connect(); // Rest of method.... } But, that has a downside, since you wouldn't be able to throw errors if you were already connected and tried to connect again on accident. Link to comment https://forums.phpfreaks.com/topic/110333-connecting-to-the-database-in-the-constructor-or-as-a-method/#findComment-566095 Share on other sites More sharing options...
Tim Fletcher Posted June 15, 2008 Author Share Posted June 15, 2008 Thanks Corbin. With regard to accidentally connecting to the database twice, the database class I'm creating is actually a singleton and so I can try to create it as many times as I like and still only have the single instance. I left it out of this example for brevity. I appreciate that you might not use the actual database connection and therefore there's no point in creating it until you actually need it. I just can't quite see that you'd create a database object if you actually didn't intend on using it. I mean databases and web apps are virtually one and the same! Maybe my limited experience is showing a bit on this one. Is the overhead of connecting that significant? Maybe I could write some kind of test. Oh, and thanks for the streamlined code. Cheers! Link to comment https://forums.phpfreaks.com/topic/110333-connecting-to-the-database-in-the-constructor-or-as-a-method/#findComment-566110 Share on other sites More sharing options...
corbin Posted June 15, 2008 Share Posted June 15, 2008 Well.... For example, consider you have a bunch of pages on a site, and then you have maybe a Contact Us page.... In some situations, would it be necessary to store the contact information in a database? So, why connect to the database on that page? As far as performance goes, the gain would be in the miliseconds by far.... Another thing.... If you do connect to the database every page, then you have to error handle every page. I guess really it could be seen as personal preference. Link to comment https://forums.phpfreaks.com/topic/110333-connecting-to-the-database-in-the-constructor-or-as-a-method/#findComment-566149 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.