Guest User

Untitled

a guest
Mar 15th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.85 KB | None | 0 0
  1.     <?php
  2.     //ela herdará os métodos e atributos do PDO através da palavra-chave extends
  3.     class Conexao extends PDO {
  4.     private $dsn = ‘pgsql:host=localhost;port=5432;dbname=agenda’;
  5.     private $user = ‘usuario’;
  6.     private $password = ‘senha’;
  7.     public $handle = null;
  8.     function __construct() {
  9.     try {
  10.     //aqui ela retornará o PDO em si, veja que usamos parent::_construct()
  11.     if ( $this->handle == null ) {
  12.     $dbh = parent::__construct( $this->dsn , $this->user , $this->password );
  13.     $this->handle = $dbh;
  14.     return $this->handle;
  15.     }
  16.     }
  17.     catch ( PDOException $e ) {
  18.     echo ‘Conexão falhou. Erro:. $e->getMessage( );
  19.     return false;
  20.     }
  21.     }
  22.     //aqui criamos um objeto de fechamento da conexão
  23.     function __destruct( ) {
  24.     $this->handle = NULL;
  25.     }
  26.     }
  27.     ?>
Add Comment
Please, Sign In to add comment