Skip to content

Instantly share code, notes, and snippets.

@Chen-Michael
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save Chen-Michael/a1a19960878875bb836d to your computer and use it in GitHub Desktop.

Select an option

Save Chen-Michael/a1a19960878875bb836d to your computer and use it in GitHub Desktop.
DbSelector
<?php
class DbAccountFactory{
/**
* __construct
* Ban New The Class
*
* @access Private
* @author Michael-Chen
* @version 1.0
*/
private function __construct(){}
/**
* getAccountModel
* Get Database Account Model
*
* @access Public
* @author Michael-Chen
* @param string $type Select The Database Account Model Want To New
* @return object Account Model
* @version 1.0
*/
public static function getAccountModel($type){
switch(strtolower($type)){
case "main":
return new DbAccountModel("account", "password", "mysql", "127.0.0.1");
}
return null;
}
}
class DbAccountModel{
/** @var string Database Account */
private $mAccount = "";
/** @var string Database Password */
private $mPassword = "";
/** @var string Database Name */
private $mName = "";
/** @var string Database Hosting */
private $mHost = "";
/**
* __construct
* Setting The Account Info
*
* @access Public
* @author Michael-Chen
* @param string $account Database Account
* @param string $password Database Password
* @param string $name Database Name
* @param string $host Database Address
* @version 1.0
*/
public function __construct($account, $password, $name, $host){
$this->mAccount = $account;
$this->mPassword -> $password;
$this->mName -> $name;
$this->mHost -> $host;
}
/**
* getAccount
* Get Database Account Value
*
* @access Public
* @author Michael-Chen
* @return string Database Account Value
* @version 1.0
*/
function getAccount(){
return $this->mAccount;
}
/**
* getPassword
* Get Database Password Value
*
* @access Public
* @author Michael-Chen
* @return string Database Password Value
* @version 1.0
*/
function getPassword(){
return $this->mPassword;
}
/**
* getName
* Get Database Name Value
*
* @access Public
* @author Michael-Chen
* @return string Database Name Value
* @version 1.0
*/
function getName(){
return $this->mName;
}
/**
* getHost
* Get Database Host Value
*
* @access Public
* @author Michael-Chen
* @return string Database Host Value
* @version 1.0
*/
function getHost(){
return $this->mHost;
}
}
?>
<?php
include_once("db_account_factory.php");
class DbSelector{
/** @var object[] PDO Entity */
private static $mPdoConnection = array();
/** @var string Database Account Type */
private static $mAccountType = "main";
/**
* __construct
* Ban New The Class
*
* @access Private
* @author Michael-Chen
* @version 1.0
*/
private function __construct(){}
/**
* setAccountType
* Set Database Account Type
*
* @access Public
* @author Michael-Chen
* @param string $type Select The Database Account Want To Use
* @version 1.0
*/
public static function setAccountType($type){
self::$mAccountType = $type;
}
/**
* getMySqlPdoConnection
* Get MySql PDO Connection Entity
*
* @access Public
* @author Michael-Chen
* @return object PDO Entity|Null
* @throws Exception If The Variable 'dbAccountModel' Is Null
* @version 1.0
*/
public static function getMySqlPdoConnection(){
if(!isset(self::$mPdoConnection[self::$mAccountType])){
try{
$dbAccountModel = DbAccountFactory::getAccountModel(self::$mAccountType);
$connectionInfo = "mysql:host=".$dbAccountModel -> getHost().";dbname=".$dbAccountModel -> getName();
self::$mPdoConnection[self::$mAccountType] = new PDO(
$connectionInfo,
$dbAccountModel -> getAccount(),
$dbAccountModel -> getPassword()
);
self::$mPdoConnection[self::$mAccountType] -> query("SET NAMES UTF8");
}catch (PDOException $pe) {
error_log("PDO Connection Failed", 0);
}catch (Exception $e) {
error_log("Variable 'dbAccountModel' Is Null", 0);
}
}
return self::$mPdoConnection[self::$mAccountType];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment