Advertisement
Guest User

Untitled

a guest
May 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.74 KB | None | 0 0
  1. <?php
  2. /**
  3.  *
  4.  * @author Raul Souza Silva
  5.  * @copyright none
  6.  *
  7.  */
  8. Class PDOConnectionFactory {
  9.     /**
  10.      * @var $bdType String
  11.      * @uses is the what database you will connect
  12.      */
  13.     private $dbType      = "mysql"; // Change to connect to others databases
  14.     /**
  15.      * @var $host String
  16.      * @uses is the server you will connect
  17.      */
  18.     private $host        = "127.0.0.1";
  19.     /**
  20.      * @var $username String
  21.      * @uses the user will connect to database
  22.      */
  23.     private $username    = "root";
  24.     /**
  25.      * @var $password String
  26.      * @uses is the user's password of the database
  27.      */
  28.     private $password    = "";
  29.     /**
  30.      * @var $database String
  31.      * @uses wich data base you will connect
  32.      */
  33.     private $database    = "estudos";
  34.     /**
  35.      * @var $con ObjectConnection
  36.      * @uses this var will store the connection's object of the database
  37.      */
  38.     private $con         = null;
  39.     /**
  40.      * @var $persistent boolean
  41.      * @uses set this to true to able the persistent connection
  42.      */
  43.     private $persistent  = false;
  44.    
  45.     public function PDOConnectionFactory($persistent=false){
  46.         if($persistent!=false){$this->persistent = true;}
  47.     }
  48.    
  49.     public function setDbType($type){$this->dbType = $type;}
  50.     public function setHost($host){$this->host = $host;}
  51.     public function setUsername($user){$this->username = $user;}
  52.     public function setPassword($pass){$this->password = $pass;}
  53.    
  54.     public function getConnection(){
  55.         try {
  56.             $this->con = new PDO($this->dbType.":host=".$this->host.";dbname=".$this->database,$this->username,$this->password,array(PDO::ATTR_PERSISTENT=>$this->persistent));
  57.             return $this->con;
  58.         } catch(PDOException $e){
  59.             echo "Erro: ".$e->getMessage();
  60.         }
  61.     }
  62.    
  63.     public function close(){
  64.         if($this->con!=null){
  65.             $this->con = null;
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement