Skip to main content
Tweeted twitter.com/StackProgrammer/status/749194186847621120
added 33 characters in body
Source Link
user201263
user201263

The PHP codebase I'm working on has an utility class that returns a database connection. The implementation looks like this:

class Database { private static $conn; private static $init = false; public static function connect() { if (!self::$init) { $cnf = new Configuration(); self::$conn = new DatabaseConnection($cnf->get('dbname'), $cnf->get('username'), $cnf->get('password'));   self::$init = true; }   return self::$conn; } } 

Things have been done this way to apparently spend lesser time on opening connections, which occur over a network.

Is instantiating a single database connection like this and returning that instance to all callers a good software engineering practice?

The PHP codebase I'm working on has an utility class that returns a database connection. The implementation looks like this:

class Database { private static $conn; private static $init = false; public static function connect() { if (!self::$init) { $cnf = new Configuration(); self::$conn = new DatabaseConnection($cnf->get('dbname'), $cnf->get('username'), $cnf->get('password')); } return self::$conn; } 

Things have been done this way to apparently spend lesser time on opening connections, which occur over a network.

Is instantiating a single database connection like this and returning that instance to all callers a good software engineering practice?

The PHP codebase I'm working on has an utility class that returns a database connection. The implementation looks like this:

class Database { private static $conn; private static $init = false; public static function connect() { if (!self::$init) { $cnf = new Configuration(); self::$conn = new DatabaseConnection($cnf->get('dbname'), $cnf->get('username'), $cnf->get('password'));   self::$init = true; }   return self::$conn; } } 

Things have been done this way to apparently spend lesser time on opening connections, which occur over a network.

Is instantiating a single database connection like this and returning that instance to all callers a good software engineering practice?

Source Link
user201263
user201263

Is it good practice to use a single database connection?

The PHP codebase I'm working on has an utility class that returns a database connection. The implementation looks like this:

class Database { private static $conn; private static $init = false; public static function connect() { if (!self::$init) { $cnf = new Configuration(); self::$conn = new DatabaseConnection($cnf->get('dbname'), $cnf->get('username'), $cnf->get('password')); } return self::$conn; } 

Things have been done this way to apparently spend lesser time on opening connections, which occur over a network.

Is instantiating a single database connection like this and returning that instance to all callers a good software engineering practice?