0

I am new to Php OOP and I doing research I read that the singleton design for database objects are the preferred method. However, I am lost as to whether this will continue to make new instances of that connection, since I am not directly creating the singleton class but using a framework provided through PEAR?

abstract class WI_Object_DB extends WI_Object {
protected static $cxn;

protected static function init($config){
    self::$cxn =& MDB2::singleton($config->dsn);
    if (!PEAR::isError(self::$cxn)) {
        self::$cxn->setFetchMode('DB_FETCHMODE_ASSOC');
    } else {
        throw new Exception(self::$cxn->getMessage());
    }
}

public function __construct()
{
    parent::__construct();
    if (self::$cxn===null) {
        self::init($GLOBALS['configs']);
    }

}

}

Kevlwig
  • 133
  • 2
  • 11
  • Using a Singleton pattern for a DB connection is actually the *opposite* of recommended because: 1. If you ever need a second DB connection you're screwed. 2. Singletons are essentially global state which makes testing and debugging much more difficult. – Sammitch Feb 17 '18 at 01:35
  • @Sammitch so scratch the singleton? – Kevlwig Feb 17 '18 at 01:38

0 Answers0