1
In college we learn to use the model FACTORY
for connections, but I wonder if it is possible to apply interfaces too, as in the following example:
Case study
I have a php application that follows the pattern as closely as possible MVC
, but that should have the independent connection, that is, if it is necessary to change the bank, there is no need to rewrite the whole application.
So, it was created in the model, a class called MySQ_LConnect
, providing connection to the bank Mysql
, and the implementation of querys
passed down to her:
class MYSQL_connect {
private $hostname = 'myHost';
private $username = 'DBUser';
private $senha = '';
private $banco = 'database';
private $conn;
public function conectar(){
$this->conn = mysql_pconnect($this->hostname, $this->username, $this->senha);
$db = mysql_select_db($this->banco,$this->conn);
return $this->conn;
}
public function executarQuery($query,$conn){
$result = mysql_query($query,$conn);
return $result;
}
public function desconectar($conn){
mysql_close($conn);
}
Thinking about a future migration to the sql server
, a connection class has been created for this database and an interface, only to parameterize the two existing connection classes:
Interface
interface iConnect{
public function conectar();
public function executarQuery($query,$conexao);
public function desconectar($conexao);
}
Class of connection with sqlserver
class MSSQL_connect implements iConnect{
private $hostname = 'myHost';
private $connInfo = array("Database"=>"database", "UID"=>"DBUser", "PWD"=>"");
private $conn;
public function conectar(){
$this->conn = sqlsrv_connect($this->hostname, $this->connInfo) or die(sqlsrv_errors());
return $this->conn;
}
public function executarQuery($sql,$conn){
$result = sqlsrv_query($conn,$sql,array(),array("Scrollable"=>"buffered"));
return $result;
}
public function desconectar($conn){
sqlsrv_close($conn);
}
}
Obs.: in the class MYSQL_connect
the interface was also implemented.
Then, in the test environment, it is possible to switch between banks using a connection Fabrica
, where BANCO
is a constant in the seat prefix (just to make it easier to switch between banks):
require_once 'MySQL_connect.php';
require_once 'MSSQL_connect.php';
define("BANCO","MySQL");//ou pode ser MSSQL
class Fabrica{
public function fabricar(){
$classeDAO = BANCO."_connect";
return new $classeDAO;
}
}
Doubt
How to use interfaces in this case to abstract the type of connection, similar to that of the class Fabrica
is trying to do so that for the rest of the application does not make a difference in which bank is connecting?
If the interface does not meet this requirement well, such as abstracting without using Factory
?
Two considerations: 1 - you are returning the connection
$this->conn
and receives$conn
in some methods, which seems wrong to me, receive the connection as a parameter and you already have it inside the object; 2 - each implementation ofexecutarQuery()
returns a different type of resource, so that it makes no difference in the application you must always return the same type (a array for example). PHP does not have a native way of forcing a return type, so you should handle it manually.– Pedro Sanção
@Sanction this running Uery() thing was purposeful, I have other methods that make this conversion that you said, but I removed it so that the code would not be giant, but within each connection class I do have a method that converts the result into an array. As for variable
$conn
was a tip from the teacher, I confess that I did not even think of this side kkkk– user28595
So the only way to improve your classes would be to modify
Fabrica::fabricar()
to receive a parameter with the type of the bank, with default value, recommend using constants (const TIPO_MYSQL
)– Pedro Sanção
I edited the question,
BANCO
is a constant already.– user28595
What does this class have to do with
factory
?– Edilson
I don’t consider using Interfaces something that generalizes. Thinking is wrong. It’s more like abstraction. To generalize, is to do something that serves for any case. Abstraction is when you create simple ways to meet in a way that gives minimal conditions for a functionality.
– Ivan Ferrer
In addition, interfaces were made to bureaucratize your code, preventing other developers from destroying its basic functionality.
– Ivan Ferrer
@Ivanferrer thanks for the correction, I meant abstraction, I will correct the question.
– user28595
@Edilson in this activity, the suggestion was to use something similar to the standard Factory, in this case it was the class
Fabrica
. You’re not 100% following this pattern, it was just an inspiration.– user28595
Look, I’ll give you a very simple example of a factory.
– Edilson