Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

A quick recap from SO quick recap from SO

  1. If you want to implement the Singleton pattern (first read about SOLID, though, and pay special attention to injection), make the constructor private.

  2. Your query method is static, why? it defaults (without the user being able to do anything about this) to the last connection that was established. Are you sure that's what the user wanted? Of course not! But then again, all of the other methods, like execute exhibit the same behaviour, so Everybody will end up working on the same connection, until they run into trouble and revert to using their own instances of PDO.

As ever, everything I have to say about custom wrapper classes around an API like PDO offers, can be read here. What I think of DB wrappers in general can be found here.

If you want to have all current connections available globally, then your code should shift towards a Factory, not a Singleton pattern:

class Factory
{
 private static $connections = array();
 public static function getDB($host, array $params)
 {
 if (!isset(self::connections[$host]))
 {
 self::connections[$host] = new DB($params);
 }
 return self::connections[$host];
 }
}

A quick recap from SO

  1. If you want to implement the Singleton pattern (first read about SOLID, though, and pay special attention to injection), make the constructor private.

  2. Your query method is static, why? it defaults (without the user being able to do anything about this) to the last connection that was established. Are you sure that's what the user wanted? Of course not! But then again, all of the other methods, like execute exhibit the same behaviour, so Everybody will end up working on the same connection, until they run into trouble and revert to using their own instances of PDO.

As ever, everything I have to say about custom wrapper classes around an API like PDO offers, can be read here. What I think of DB wrappers in general can be found here.

If you want to have all current connections available globally, then your code should shift towards a Factory, not a Singleton pattern:

class Factory
{
 private static $connections = array();
 public static function getDB($host, array $params)
 {
 if (!isset(self::connections[$host]))
 {
 self::connections[$host] = new DB($params);
 }
 return self::connections[$host];
 }
}

A quick recap from SO

  1. If you want to implement the Singleton pattern (first read about SOLID, though, and pay special attention to injection), make the constructor private.

  2. Your query method is static, why? it defaults (without the user being able to do anything about this) to the last connection that was established. Are you sure that's what the user wanted? Of course not! But then again, all of the other methods, like execute exhibit the same behaviour, so Everybody will end up working on the same connection, until they run into trouble and revert to using their own instances of PDO.

As ever, everything I have to say about custom wrapper classes around an API like PDO offers, can be read here. What I think of DB wrappers in general can be found here.

If you want to have all current connections available globally, then your code should shift towards a Factory, not a Singleton pattern:

class Factory
{
 private static $connections = array();
 public static function getDB($host, array $params)
 {
 if (!isset(self::connections[$host]))
 {
 self::connections[$host] = new DB($params);
 }
 return self::connections[$host];
 }
}
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

A quick recap from SO

  1. If you want to implement the Singleton pattern (first read about SOLID, though, and pay special attention to injection), make the constructor private.

  2. Your query method is static, why? it defaults (without the user being able to do anything about this) to the last connection that was established. Are you sure that's what the user wanted? Of course not! But then again, all of the other methods, like execute exhibit the same behaviour, so Everybody will end up working on the same connection, until they run into trouble and revert to using their own instances of PDO.

As ever, everything I have to say about custom wrapper classes around an API like PDO offers, can be read here can be read here. What I think of DB wrappers in general can be found here can be found here.

If you want to have all current connections available globally, then your code should shift towards a Factory, not a Singleton pattern:

class Factory
{
 private static $connections = array();
 public static function getDB($host, array $params)
 {
 if (!isset(self::connections[$host]))
 {
 self::connections[$host] = new DB($params);
 }
 return self::connections[$host];
 }
}

A quick recap from SO

  1. If you want to implement the Singleton pattern (first read about SOLID, though, and pay special attention to injection), make the constructor private.

  2. Your query method is static, why? it defaults (without the user being able to do anything about this) to the last connection that was established. Are you sure that's what the user wanted? Of course not! But then again, all of the other methods, like execute exhibit the same behaviour, so Everybody will end up working on the same connection, until they run into trouble and revert to using their own instances of PDO.

As ever, everything I have to say about custom wrapper classes around an API like PDO offers, can be read here. What I think of DB wrappers in general can be found here.

If you want to have all current connections available globally, then your code should shift towards a Factory, not a Singleton pattern:

class Factory
{
 private static $connections = array();
 public static function getDB($host, array $params)
 {
 if (!isset(self::connections[$host]))
 {
 self::connections[$host] = new DB($params);
 }
 return self::connections[$host];
 }
}

A quick recap from SO

  1. If you want to implement the Singleton pattern (first read about SOLID, though, and pay special attention to injection), make the constructor private.

  2. Your query method is static, why? it defaults (without the user being able to do anything about this) to the last connection that was established. Are you sure that's what the user wanted? Of course not! But then again, all of the other methods, like execute exhibit the same behaviour, so Everybody will end up working on the same connection, until they run into trouble and revert to using their own instances of PDO.

As ever, everything I have to say about custom wrapper classes around an API like PDO offers, can be read here. What I think of DB wrappers in general can be found here.

If you want to have all current connections available globally, then your code should shift towards a Factory, not a Singleton pattern:

class Factory
{
 private static $connections = array();
 public static function getDB($host, array $params)
 {
 if (!isset(self::connections[$host]))
 {
 self::connections[$host] = new DB($params);
 }
 return self::connections[$host];
 }
}
added 861 characters in body
Source Link

A quick recap from SO

  1. If you want to implement the Singleton pattern (first read about SOLID, though, and pay special attention to injection), make the constructor private.

  2. Your query method is static, why? it defaults (without the user being able to do anything about this) to the last connection that was established. Are you sure that's what the user wanted? Of course not! But then again, all of the other methods, like execute exhibit the same behaviour, so Everybody will end up working on the same connection, until they run into trouble and revert to using their own instances of PDO.

As ever, everything I have to say about custom wrapper classes around an API like PDO offers, can be read here . What I think of DB wrappers in general can be found here .

If you want to have all current connections available globally, then your code should shift towards a Factory, not a Singleton pattern:

class Factory
{
 private static $connections = array();
 public static function getDB($host, array $params)
 {
 if (!isset(self::connections[$host]))
 {
 self::connections[$host] = new DB($params);
 }
 return self::connections[$host];
 }
}

A quick recap from SO

  1. If you want to implement the Singleton pattern (first read about SOLID, though, and pay special attention to injection), make the constructor private.

  2. Your query method is static, why? it defaults (without the user being able to do anything about this) to the last connection that was established. Are you sure that's what the user wanted? Of course not! But then again, all of the other methods, like execute exhibit the same behaviour, so Everybody will end up working on the same connection, until they run into trouble and revert to using their own instances of PDO

A quick recap from SO

  1. If you want to implement the Singleton pattern (first read about SOLID, though, and pay special attention to injection), make the constructor private.

  2. Your query method is static, why? it defaults (without the user being able to do anything about this) to the last connection that was established. Are you sure that's what the user wanted? Of course not! But then again, all of the other methods, like execute exhibit the same behaviour, so Everybody will end up working on the same connection, until they run into trouble and revert to using their own instances of PDO.

As ever, everything I have to say about custom wrapper classes around an API like PDO offers, can be read here . What I think of DB wrappers in general can be found here .

If you want to have all current connections available globally, then your code should shift towards a Factory, not a Singleton pattern:

class Factory
{
 private static $connections = array();
 public static function getDB($host, array $params)
 {
 if (!isset(self::connections[$host]))
 {
 self::connections[$host] = new DB($params);
 }
 return self::connections[$host];
 }
}
Source Link
Loading
default

AltStyle によって変換されたページ (->オリジナル) /