Skip to content

Instantly share code, notes, and snippets.

@Golpha
Created January 15, 2014 22:32
Show Gist options
  • Save Golpha/8446061 to your computer and use it in GitHub Desktop.
Save Golpha/8446061 to your computer and use it in GitHub Desktop.
Default Implementation of a MySQLi-Adapter Requirements: PHP 5.4 or higher
<?php
/**
* MySQLiAdapter Class
* Implements instance incubation of MySQLi
*/
class MySQLiAdapter
{
/**
* @var string
*/
private $username = '';
/**
* @var string
*/
private $password = '';
/**
* @var string
*/
private $hostname = '';
/**
* @var string
*/
private $database = '';
/**
* @var integer
*/
private $port = 3306;
/**
* @var MySQLi|Closure
*/
protected $handle = null;
/**
* Class Constructor
*
* Stores construction closure for database connection
*/
public function __construct()
{
$this->handle = function() {
$this->handle = new MySQLi(
$this->hostname,
$this->username,
$this->password,
$this->database
);
return $this->handle;
};
$this->handle = $this->handle->bindto($this, __CLASS__);
}
/**
* setHostname()
* Setter: ::$hostname
*
* @param string $string
*/
public function setHostname($string)
{
$this->hostname = (string) $string;
}
/**
* setPassword()
* Setter: ::$password
*
* @param string $string
*/
public function setPassword($string)
{
$this->password = (string) $string;
}
/**
* setUsername()
* Setter: ::$username
*
* @param string $string
*/
public function setUsername($string)
{
$this->username = (string) $string;
}
/**
* setPort()
* Setter: ::$port
*
* @param integer $integer
*/
public function setPort($integer)
{
$this->port = (integer) $integer;
}
/**
* setDatabase()
* Setter: ::$database
*
* @param string $string
*/
public function setDatabase($string)
{
$this->database = (string) $string;
}
/**
* getPassword()
* Getter: ::$database
*
* @return string
*/
public function getDatabase()
{
return $this->database;
}
/**
* getPassword()
* Getter: ::$handle.
*
* @return MySQLi
*/
public function getConnection()
{
return is_callable($this->handle) ? call_user_func($this->handle) : $this->handle;
}
/**
* ::create()
* Factory of this adapter
*
* @param string $hostname
* @param string $username
* @param string $password
* @param string $database
* @param string $port
*/
public static function create($hostname, $username, $password, $database, $port = 3306)
{
$object = new static();
$object->setHostname($hostname);
$object->setPassword($password);
$object->setDatabase($database);
$object->setUsername($username);
$object->setPort($port);
return $object;
}
}
@endrureza
Copy link

Well, one to go. Thank's man.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment