Abstraction of the PHP PDO Connection class

Asked

Viewed 441 times

1

Staff I have the following class for database connection...

namespace App\Database;

class Conn
{
    public static function getDb()
    {
        return new \PDO('mysql:host=localhost;dbname=dkse','root','root');
    }
}

It’s a very simple class and it’s working at the moment.. however in the future in my project I will probably need to use another database for example MSSQL, I would like to know how to implement an interface.. for me to simply implement the connection classes of my project following the principles of SOLID.. thank you!

  • 1

    I do not know how MSSQL works, but considering that all dependencies are installed, it would not just change the parameter in the PDO instance?

1 answer

1

You can create a Connections class that would be your interface to the database in front of your CONN class, within which constructor to define what type of database you will use. After instantiating the Connections class according to the database you are going to access, simply create the methods in both classes with the same name, you should use the same name in the methods in both classes that handle queries. This will avoid flow deviations in your code.

Or create an Abstract class and inherit in its CONN class and within the Abstract class develop the method that defines which type of database will use making CONN access the methods also for handling the querys in both types of databases.

Suggestion for future maintenance purpose, the first way is more efficient.

I have some systems that work supporting older versions of the same so if there are still doubts I can post an example in php working.

I’ll set a simple example :

Arquivo Classe Modelo Connections 

class Connections{

   public function __construct($TDatabase){   
         if(strcasecmp($TDatabase,"MYSQL") == 0){
             $CONN_MSSQL  = new MSSQL();
         }else{
             $CONN_MYSQL  = new MYSQL();
         }
    }
}

MSSQL ARQUIVO CONSTANTES mssql.var.php
define('HOST','ip de acesso ao BD');
define('Porta','porta que seu banco de dados trabalha');
define('user','usuario de acesso ao banco');
define('senha','senha de acesso ao banco');


Arquivo Classe Modelo MSSQL

include mssql.var.php

class MSSQL{

     public function __construct(){
        // estabelece conexão com o banco MSSQL, utilize as constantes 
        //definidas no arquivo var     
     }

     public function GetAllUsuarios(){
        /* metodo executa querie no banco de dados MSSQL pegando todos os 
           clientes cadastrados*/
     }
}


MYSQL ARQUIVO CONSTANTES mysql.var.php
define('HOST','ip de acesso ao BD');
define('Porta','porta que seu banco de dados trabalha');
define('user','usuario de acesso ao banco');
define('senha','senha de acesso ao banco');



Arquivo Classe Modelo MYSQL

include mysql.var.php
class MYSQL{

     public function __construct(){
        // estabelece conexão com o banco Mysql, utilize as constantes 
        //definidas no arquivo var     
     }

     public function GetAllUsuarios(){
        /* metodo executa querie no banco de dados MYSQL pegando todos os 
           clientes cadastrados*/
     }
}

Ready built a very simple operating structure just to increase understanding to access the bank you should call any interface of your system in this way :

$Connections   = new Connections('MYSQL');
$listaUsuarios = $Connections->GetAllUsuarios();
print_r($listaUsuarios);

Note that I am reading the MYSQL data because I informed in the constructor of the Connections class the database I wanted to work on.

This form maintains the independence of the data, that is, it separates the system from the type of database it is using. It also reduces coupling in the system.

Follow connection in Singleton standard Conexão singleton

I’m happy to help Solomon hugs. = P

  • Thanks, however I am using MVC, and the connection I inject into a container.. to use in models.. and the class needs to be static =X

  • Ok in this case you can take the connection from within the MSSQL and MYSQL class and create a new layer behind using the Static connection in Singleton standard

  • Following Everton’s tip I edited the answer to make it more organized!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.