Advertisement
Googleinurl

DB CLASS CONEXÃO PDO

Apr 17th, 2013
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.90 KB | None | 0 0
  1. <?php
  2.  
  3. class Dados {
  4.     /* MÉtodo construtor do banco de dados */
  5.  
  6.     private $db;
  7.  
  8.     function __construct($db) {
  9.         $this->db = $db;
  10.     }
  11.  
  12.     /* Evita que a classe seja clonada */
  13.  
  14.     public function __clone() {
  15.        
  16.     }
  17.  
  18.     public function __destruct() {
  19.         $this->disconnect();
  20.         foreach ($this as $key => $value) {
  21.             unset($this->$key);
  22.         }
  23.     }
  24.  
  25.     private static $banco = array('dbtype' => 'mysql',
  26.         'host' => '172.16.0.8',
  27.         'port' => '3306',
  28.         'user' => 'root',
  29.         'password' => '102030@xdrfg',
  30.     );
  31.  
  32.     /* Metodos que trazem o conteudo da variavel desejada
  33.       @return   $xxx = conteudo da variavel solicitada */
  34.  
  35.     private function getDBType() {
  36.         return self::$banco['dbtype'];
  37.     }
  38.  
  39.     private function getHost() {
  40.         return self::$banco['host'];
  41.     }
  42.  
  43.     private function getPort() {
  44.         return self::$banco['port'];
  45.     }
  46.  
  47.     private function getUser() {
  48.         return self::$banco['user'];
  49.     }
  50.  
  51.     private function getPassword() {
  52.         return self::$banco['password'];
  53.     }
  54.  
  55.     private function getDB() {
  56.         return $this->db;
  57.     }
  58.  
  59.     public function connect() {
  60.         try {
  61.             $this->conexao = new PDO($this->getDBType() .
  62.                     ":host=" . $this->getHost() .
  63.                     ";port=" . $this->getPort() .
  64.                     ";dbname=" . $this->getDB(), $this->getUser(), $this->getPassword());
  65.             $this->conexao->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  66.             $this->conexao->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  67.         } catch (PDOException $i) {
  68.  
  69.             die("Erro: <code>" . $i->getMessage() . "</code>"); //erro
  70.         }
  71.         return ($this->conexao);
  72.     }
  73.  
  74.     public function disconnect() {
  75.         $this->conexao = null;
  76.     }
  77.  
  78. }
  79.  
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement