2
The connection part is ready:
<?php
/*
* O padrão Singleton
*/
abstract class Conexao {
const USER = "root";
const PASS = "";
private static $instance = null;
private static function conectar() {
try { ##Tenta conectar ao banco se não funcionar cai no catch
if (self::$instance == null): ##se não existir conexão com PDO receber a new PDO() | a conexão
$dsn = "mysql:host=localhost;dbname=phpoo";
self::$instance = new PDO($dsn, self::USER, self::PASS);
endif;
} catch (PDOException $e) {
echo "Erro: " . $e->getMessage(); #
}
return self::$instance; ## se ja exixtir uma conexão ele retorna a conexão
}
protected static function getDB() {
return self::conectar();
}
}
create an Abstract.php class (do the Generic CRUD here) with:
<?php
abstract class Abstrata extends Conexao {
protected function listar($where) {
//implemente aqui seu código padrão para fazer update
$db = parent::getDB();
}
protected function alterar($id, $data) {
//implemente aqui seu código padrão para fazer update
$db = parent::getDB();
}
protected function deletar($id) {
//implemente aqui seu código padrão para fazer delete
$db = parent::getDB();
}
protected function cadastrar($data) {
//implemente aqui seu código padrão para fazer insert
$db = parent::getDB();
//outros códigos
}
}
And deposi only call external methods to the other classes. Will that way of implementing like this ?
Could you put your code and describe the difficulty/problem you are facing? take advantage and see how the site works on tour and how to ask
– rray
How do I enter code here?
– JB_
Okay. Code on screen
– JB_
Sincerity, what is the question @Kall.max? I don’t understand, if you can explain?
– Maria
This CRUD that I am developing is Orietado the Object would like to make some modifications to it to be safer using PDO, ie stacking PDO on it. That’s what I’m not getting.
– JB_
I think this is a ready-made code that you’re asking someone to modify it to suit your needs. Please follow the advice of @Lost and formulate so that we can help you on what you tried to do.
– Marcos Vinicius
I’m not asking you to modify, , but to help me convert it to PDO. An improvement, As for the above code, it is my usefulness, I did, but taking a course on the Internet I saw the benefits of PDO and reading the http://php.net/manual/en/book.pdo.php documentation, I obeyed their benefic degree and Usuability decide to migrate to such for that reason asked help here.
– JB_
It is difficult to adapt without knowing how you will use the class... Put an example of use that you use.
– Papa Charlie
I am working here and trying to develop. I will put yes. Just finish some things here. Because what I’m trying to develop is a Generic CRUD with PDO on top of this existing OO, I’m trying to adapt. I can!! I believe.
– JB_
Take a look at this question as it may serve
– Papa Charlie
Now I will make the class where will have the CRUD classes. And later just make the calls where I want doing the instances. Could you give me an example of an Insert or select Generico with PDO, who knows.
– JB_
You want to create a Generic Persistence Model, where it saves deletes list as you pass a configuration?
– Maria
@Kall.max in my opinion this is the kind of code we should not waste time programming! What business value is this bringing to your system? I recommend using packages ready for this type of situation, in my reply I tried to teach how to use a ready DBAL if you have difficulties and interest leave a comment
– Fábio Lemos Elizandro