Dynamic connections classes 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-&gt;isInitialized()) {
            echo "Cant get instance yet because the connection isn't open";
        }
        return $this-&gt;instance;
    }

    /**
     * @return bool
     */
    public function isInitialized() {
        return $this-&gt;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-&gt;host        = $host;
        $this-&gt;database    = $database;
        $this-&gt;username    = $username;
        $this-&gt;password    = $password;
        $this-&gt;credentials = $credentials;
    }

    public final function initialize() {
        if ($this-&gt;isInitialized()) {
            throw new Exception("Database connection isnt open yet.");
        }
        $this-&gt;instance = $this-&gt;getConnectionObject();
    }

    public final function terminate() {
        if ($this-&gt;isInitialized()) {
            throw new Exception("Database is closed");
        }
        $this-&gt;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-&gt;host, $this-&gt;username, $this-&gt;password, $this-&gt;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-&gt;initialize();


To close the connection you use the following method:

$connection-&gt;terminate();




To give this DatabaseConnection functionality I call  the getInstance() method on the $connection object i've just made.

$connection-&gt;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.
Your code has a major issue. It's breaking a basic rule of inheritance. The "base" class (Connection) does not specify a constructor. That is to say, any constructors that a child class may contain should take either no, or only optional arguments. That's what the Liskov principle states anyway.

Your DatabaseConnection extends Connection (BTW: an abstract extending an abstract... that smells, if you ask me), but it defines a constructor with non-optional arguments. This contradicts, and breaks the inherited contract defined by Connection.
PHP historically, has been very forgiving when it came to conflicting signatures, especially in the constructors, but as the manual says:


  When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. Furthermore the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same. For example, if the child class defines an optional argument, where the abstract method's signature does not, there is no conflict in the signature. This also applies to constructors as of PHP 5.4. Before 5.4 constructor signatures could differ.


That ship has sailed. Now in case you're wondering why this is such a big deal, look at the code below:

function checkConnection(Connection $con)
{//type-hint ensures instance of Connection is passed
    //for some reason, create a copy:
    $class = get_class($con);//get actual class
    $newInstance = new $class();//&lt;-- No arguments!!
}


Now this function has to be allowed to assume that no arguments need to be passed to a constructor, if there even is a constructor. Simply because this function is hinting at Connection  instances. Because of the type-hint, the function can only assume the methods/properties that are declared by the Connection class are available, and that the instance that it is being passed is an instance (an extension) of this contract. Connection doesn't declare a constructor, so its children can only create a public, optional constructor.

Fix this issue ASAP.
Apart from that, I'd really like to see where you're going with this, because this abstract class Connection + abstract class DatabaseConnection seems like an awful lot of abstraction for what will, essentially, be a service locator, or DI container.

A DI Container, or SL shouldn't even care what kind of dependencies/services/objects it is injecting and instantiating. So there's even less need of these abstractions you've written from that perspective.

I'm also having doubts concerning the terminate method. If a piece of code uses a mysqli connection, and needs the connection to be open for some reason (ie: a prepared statement that is pending, a huge result set is being fetched in chunks,...), having the connections managed at a central level might cause conflicts. I'd say that the only place where the container can close connections is when it is being destroyed: if the container is going out of scope, all that it has spawned is probably gone, too.



Update - A crude example of a container:

Say you have config files, that define DB connections, which could look like this;

connections:
    db:
        readConnection:
            class: mysqli
            host: 127.0.0.1
            database: db
            username: user
            password: pass
        writeConnection:
            class: PDO
            host: 8.8.8.8
            database: db
            username: user
            password: pass


Now, a container would parse this config, and allow you to write something like this:

$mysqli = $container-&gt;get('connections.db.readConnection');


This get method would then parse the slug, and interpret the configuration it finds, to end up returning:

$path = explode('.', $slug);
$section = $this-&gt;getFullConfig();
foreach ($path as $key)
    $section = $section-&gt;get($key);
$class = $section-&gt;get('class');
return new $class(
    $section-&gt;get('host'),
    $section-&gt;get('database'),
    $section-&gt;get('username'),
    $section-&gt;get('password')
);


Note that this is a grossly oversimplified, but you get the idea.