/** * Factory method. * Tries to return instance of db in global space, if exists * @return \PDO Returns a singleton PDO instance ($db). */ public static function factory() { global $db; if (isset($db) && is_object($db) && get_class($db) == __CLASS__) { return $db; } $config = Config::getInstance()->get('db'); try { $pdo = new \PDO("mysql:host={$config['host']};dbname={$config['name']}", $config['user'], $config['pass']); $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } catch (PDOException $e) { var_dump($e); } $obj = new Model(); return $obj->setDb($pdo); }