Skip to content

Instantly share code, notes, and snippets.

@Simplesmente
Last active January 20, 2016 15:14
Show Gist options
  • Save Simplesmente/4b79689e23f3b431fd4f to your computer and use it in GitHub Desktop.
Save Simplesmente/4b79689e23f3b431fd4f to your computer and use it in GitHub Desktop.
<?php
/**
* Conn.class [ CONEXÃO ]
* Classe abstrata de conexão. Padrão SingleTon.
* Retorna um objeto PDO pelo método estático getConn();
*
* @copyright (c) 2013, Robson V. Leite UPINSIDE TECNOLOGIA
*/
abstract class Conn {
private static $Host = HOST;
private static $User = USER;
private static $Pass = PASS;
private static $Dbsa = DBSA;
/** @var PDO */
private static $Connect = null;
/**
* Conecta com o banco de dados com o pattern singleton.
* Retorna um objeto PDO!
*/
private static function Conectar() {
try {
if (self::$Connect == null):
$dsn = 'mysql:host=' . self::$Host . ';dbname=' . self::$Dbsa;
$options = [ PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8'];
self::$Connect = new PDO($dsn, self::$User, self::$Pass, $options);
endif;
} catch (PDOException $e) {
echo $e->getMessage();
die;
}
self::$Connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return self::$Connect;
}
/** Retorna um objeto PDO Singleton Pattern. */
protected static function getConn() {
return self::Conectar();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment