Advertisement
Guest User

PHP Database for PDO(+FluentPDO)

a guest
Aug 13th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.82 KB | None | 0 0
  1. <?php
  2.  
  3. require_once (__DIR__ . '/FluentPDO/FluentPDO.php');
  4.  
  5. /**
  6.  * このクラスは FluentPDO というライブラリを利用しているので
  7.  * 公式サイトからダウンロードして設置してください。
  8.  * http://envms.github.io/fluentpdo/
  9.  */
  10.  
  11. class db {
  12.     private $pdo;
  13.     private $fpdo;
  14.     protected $dsn;
  15.  
  16.     // データベースの情報を格納するための変数
  17.     protected $host, $username, $password, $db, $charset;
  18.  
  19.     public function __construct($host = null, $username = null, $password = null, $db = null, $charset = null)
  20.     {
  21.         // データベースの情報
  22.         $this->host = $host;
  23.         $this->username = $username;
  24.         $this->password = $password;
  25.         $this->db = $db;
  26.         $this->charset = $charset;
  27.         // FluentPDO オブジェクトの生成
  28.         $this->fpdo = $this->connectDb();
  29.     }
  30.     private function  connectDb() {
  31.         // データソース名の指定 (MySQL)
  32.         $dsn = sprintf("mysql:host=%s;dbname=%s;charset=%s",
  33.             $this->host, $this->db,  $this->charset);
  34.         try{
  35.             // PDOオブジェクトの生成
  36.             $this->pdo = new PDO($dsn, $this->username, $this->password);
  37.             return new FluentPDO($this->pdo);
  38.         }catch (PDOException $e){
  39.             echo $e->getMessage();
  40.             exit;
  41.         }
  42.     }
  43.  
  44.     public function getFPDO(){
  45.         return $this->fpdo;
  46.     }
  47.  
  48.     public function __call($name = null, $args = null)
  49.     {
  50.         if (!is_object($this->fpdo)) {
  51.             return false;
  52.         }
  53.  
  54.         if (method_exists($this->fpdo, $name)) {
  55.             return call_user_func_array(array($this->fpdo, $name), $args);
  56.         }
  57.     }
  58.  
  59.     function __destruct()
  60.     {
  61.         unset($this->fpdo);
  62.         unset($this->pdo);
  63.     }
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement