Abstraction of the Connection PHP PDO class

1

Personal I have the following class for connecting to database ...

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 right now ... but in the future I'll probably need to use another database for example MSSQL, I'd like to know how to implement an interface .. for me to simply implement the connection classes of my project following SOLID principles .. thanks!

    
asked by anonymous 04.07.2017 / 00:13

1 answer

1

You can create a Connections class that would interface with your database in front of your CONN class, inside of it in the constructor to define which type of database to use. After you instantiate the Connections class according to the database you are accessing, just create the methods in both classes with the same name, you must use the same name in the methods in both querying classes. This will prevent biases in your code.

Or create an abstract class and inherit in your CONN class and within the abstract class develop the method that defines which type of database will be used making CONN access the methods also for handling the queries in both types of banks

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

I have some systems that work supporting older versions of it so if there are still questions I can post an example in php running.

I'll put together 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 to build a very simple structure of operation just to increase the 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);

Notice that this is how I am reading the MYSQL data because I gave the Connections class constructor the bank I wanted to work on.

This form maintains the independence of the data, ie separates the system from the type of database you are using. It also reduces the coupling in the system.

Follow connection in singleton pattern

I'm happy to help Hugs Solomon. = P

    
04.07.2017 / 03:43