I have an abstract Connection
class which looks like this:
abstract class Connection
{
/**
* @var mixed $instance Represents the instance of the connection
*/
protected $instance = null;
/**
* @return mixed
*/
public function getInstance() {
if (!$this->isInitialized()) {
echo "Cant get instance yet because the connection isn't open";
}
return $this->instance;
}
/**
* @return bool
*/
public function isInitialized() {
return $this->instance !== null;
}
/**
* Initializes the connection
*/
public abstract function initialize();
/**
* Closes the connection
*/
public abstract function terminate();
}
It requires an intialize()
and terminate()
in its childs or subchilds.
- The initialize method opens the connection
- The terminate method closes the connection
The $instance
determinates the object which has to be ran to start the Connection
. For example, for a MySQL connection, this could be the mysqli
object with its parameters.
A child class of the abstract Connection
class looks like this (in this case im only showing a Connection to a database).
This class cannot be instantiated because its also abstract.
abstract class DatabaseConnection extends Connection
{
/**
* @var string
*/
protected $host;
/**
* @var
*/
protected $database;
/**
* @var
*/
protected $username;
/**
* @var
*/
protected $password;
/**
* @var
*/
protected $credentials = [];
/**
* @param float $host
* @param string $database
* @param string $username
* @param string $password
* @param array $credentials
*/
public function __construct($host, $database, $username, $password, $credentials = []) {
$this->host = $host;
$this->database = $database;
$this->username = $username;
$this->password = $password;
$this->credentials = $credentials;
}
public final function initialize() {
if ($this->isInitialized()) {
throw new Exception("Database connection isnt open yet.");
}
$this->instance = $this->getConnectionObject();
}
public final function terminate() {
if ($this->isInitialized()) {
throw new Exception("Database is closed");
}
$this->instance = null;
}
protected abstract function getConnectionObject();
}
This mostly speaks for itself, you initialize the credentials in the __construct
method.
The getConnectionObject()
determinates the object wich has to be instantiated to initialize a Database Connection.
A child class of the DatabaseConnection
looks like this:
class MySQLConnection extends DatabaseConnection
{
/**
* @return mysqli
*/
public function getConnectionObject() {
return new mysqli($this->host, $this->username, $this->password, $this->database);
}
}
This can be instantiated, to use this, i need to use the following code:
$connection = new MySQLConnection('127.0.0.1', 'db', 'username' 'password');
To open the connection you use the following method:
$connection->initialize();
To close the connection you use the following method:
$connection->terminate();
To give this DatabaseConnection
functionality I call the getInstance()
method on the $connection
object i've just made.
$connection->getInstance();
In this case this returns the mysqli
object, from here i can call all needed functions.
A few notes:
- This is for practice, i was trying to create a connection class wich had support for multiple types of connections.
- What I originally wanted was to seperate each database function in a seperate class, but i did not know how to do this.