You end up creating two instances (or more) of the PDO class, in which case one would be sufficient for bank operations. There is a model and I will share, with Injection of Dependencies and layer DAL.
Interfaces
interface ConnectionInterface {
public function Close();
public function Connection();
}
interface ClienteInterface {
public function setId($value);
public function setNome($value);
public function getId();
public function getNome();
}
interface FornecedorInterface {
public function setId($value);
public function setRazaoSocial($value);
public function getId();
public function getRazaoSocial();
}
Implementing these Interfaces
class Connection implements ConnectionInterface {
private $db;
public function _construct(){
$options = array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING);
$dsn = 'mysql:host=localhost;dbname=dbtest';
$this->db = new PDO($dsn, 'root', 'senha', $options);
}
public function Close(){
unset($this->db);
}
public function Connection(){
return $this->db;
}
}
class Cliente implements ClienteInterface {
private $id;
private $nome;
public function __construct($id = 0, $nome = ''){
$this->id = $id;
$this->nome = $nome;
}
public function setId($value){
$this->id = $value;
}
public function setNome($value){
$this->nome = $value;
}
public function getId(){
return $this->id;
}
public function getNome(){
return $this->nome;
}
}
class Fornecedor implements FornecedorInterface {
private $id;
private $razaoSocial;
public function __construct($id = 0, $razaoSocial = ''){
$this->id = $id;
$this->razaoSocial = $razaoSocial;
}
public function setId($value){
$this->id = $value;
}
public function setRazaoSocial($value){
$this->razaoSocial = $value;
}
public function getId(){
return $this->id;
}
public function getRazaoSocial(){
return $this->razaoSocial;
}
}
DAL
class DalCliente {
private $Connection;
public function _construct(ConnectionInterface $Connection){
$this->Connection = $Connection;
}
public function Insert(ClienteInterface $cliente){
$sts = $this->Connection->prepare('INSERT INTO tbcliente(nome) values(?)');
$sts->bindValue(1, $cliente->getNome(),PDO::PARAM_STR);
$sts->execute();
$cliente->setId($this->Connection->lastInsertId());
return $cliente;
}
public function Edit(ClienteInterface $cliente){
$sts = $this->Connection->prepare('UPDATE tbcliente SET nome=? WHERE id=?');
$sts->bindValue(1, $cliente->getNome(),PDO::PARAM_STR);
$sts->bindValue(2, $cliente->getId(),PDO::PARAM_INT);
$sts->execute();
}
}
class DalFornecedor {
private $Connection;
public function _construct(ConnectionInterface $Connection){
$this->Connection = $Connection;
}
public function Insert(FornecedorInterface $fornecedor){
$sts = $this->Connection->prepare('INSERT INTO tbfornecedor(nome) values(?)');
$sts->bindValue(1, $fornecedor->getRazaoSocial(),PDO::PARAM_STR);
$sts->execute();
$cliente->setId($this->Connection->lastInsertId());
return $fornecedor;
}
public function Edit(FornecedorInterface $fornecedor){
$sts = $this->Connection->prepare('UPDATE tbfornecedor SET razaosocial=? WHERE id=?');
$sts->bindValue(1, $fornecedor->getRazaoSocial(),PDO::PARAM_STR);
$sts->bindValue(2, $fornecedor->getId(),PDO::PARAM_INT);
$sts->execute();
}
}
How to use
Let’s say that you in a code block, or in a controller have to open two different DAL SQL, do:
$connection = new Connection();
$dalcliente = new DalCliente($connection);
$dalfornecedor = new DalFornecedor($connection);
Insert operation
$fornecedor = new Fornecedor(0, "Fornecedor 1");
$fornecedor = $dalfornecedor->Insert($fornecedor); // inserindo fornecedor
Right now you are injecting a Class that is responsible for connection with bank in two other classes, eliminating the duplication of instances, improving performance, standardizing its software to facilitate new implementations and corrections.
Folder organization
Folder Connection: Connection.php
DAL folder: DalCliente.php
and DalFornecedor.php
Folder Interfaces: ConnectionInterface.php
, ClienteInterface.php
and FornecedorInterface.php
Pasta Poco: Cliente.php
and Fornecedor.php
To call !!!
Create the files at the root and include what you need:
include 'Interfaces/ConnectionInterface.php';
include 'Connection/Connection.php';
include 'Interfaces/ClienteInterface.php';
include 'Interfaces/FornecedorInterface.php';
include 'Poco/Cliente.php';
include 'Poco/Fornecedor.php';
include 'Dal/DalCliente.php';
include 'Dal/DalFornecedor.php';
For an example of injection at this link: http://answall.com/a/16771/6026
– user6026