Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once (__DIR__ . '/FluentPDO/FluentPDO.php');
- /**
- * このクラスは FluentPDO というライブラリを利用しているので
- * 公式サイトからダウンロードして設置してください。
- * http://envms.github.io/fluentpdo/
- */
- class db {
- private $pdo;
- private $fpdo;
- protected $dsn;
- // データベースの情報を格納するための変数
- protected $host, $username, $password, $db, $charset;
- public function __construct($host = null, $username = null, $password = null, $db = null, $charset = null)
- {
- // データベースの情報
- $this->host = $host;
- $this->username = $username;
- $this->password = $password;
- $this->db = $db;
- $this->charset = $charset;
- // FluentPDO オブジェクトの生成
- $this->fpdo = $this->connectDb();
- }
- private function connectDb() {
- // データソース名の指定 (MySQL)
- $dsn = sprintf("mysql:host=%s;dbname=%s;charset=%s",
- $this->host, $this->db, $this->charset);
- try{
- // PDOオブジェクトの生成
- $this->pdo = new PDO($dsn, $this->username, $this->password);
- return new FluentPDO($this->pdo);
- }catch (PDOException $e){
- echo $e->getMessage();
- exit;
- }
- }
- public function getFPDO(){
- return $this->fpdo;
- }
- public function __call($name = null, $args = null)
- {
- if (!is_object($this->fpdo)) {
- return false;
- }
- if (method_exists($this->fpdo, $name)) {
- return call_user_func_array(array($this->fpdo, $name), $args);
- }
- }
- function __destruct()
- {
- unset($this->fpdo);
- unset($this->pdo);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement