Class to handle connections in the database

1

Some time ago I created the class below to do CRUD operations on the database.

Based on good programming practices, what would you change in class? and why?

<?php        
    Banco::getConexao();

    abstract class Banco {
        private static $database = 'mysql';
        private static $host = 'localhost';
        private static $banconome = 'testes';
        private static $user = 'root';
        private static $pass = '';
        private static $conexao = NULL;

        public function __construct() { }

        public function __destruct(){
            self::$conexao = NULL;
        }// __destruct

        public static function getConexao(){
            $dsn = self::$database.':host='.self::$host.';dbname='.self::$banconome;
            try{
                if(is_null(self::$conexao)):
                    self::$conexao = new PDO($dsn, self::$user, self::$pass);
                    self::$conexao->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
                endif;
                return self::$conexao;
            }catch(PDOException $e){
                self::erroLog(__FILE__, __FUNCTION__, $e->getMessage());
            }   
        }// getConexao

        public static function insert($tabela, $dados){
            $sql = 'INSERT INTO '.$tabela.' (';
            foreach($dados as $key => $value):
                $campos[] = $key;
                $tokens[] = '?';
                $valores[] = $value;
            endforeach;
            try{
                $sql .= implode(', ', $campos).') VALUES ('.implode(', ', $tokens).')';
                $query = self::$conexao->prepare($sql);
                $query->execute($valores);
            }catch(PDOException $e){
                self::erroLog(__FILE__, __FUNCTION__, $e->getMessage());
            }
        }// insert

        public static function selectAll($tabela, $condicao = NULL){
            $sql = 'SELECT * FROM '.$tabela;
            if(!is_null($condicao)) $sql .= ' WHERE '.$condicao;
            try{
                $query = self::$conexao->prepare($sql);
                $query->execute();
                return $query->fetchAll(PDO::FETCH_OBJ);
            }catch(PDOException $e){
                self::erroLog(__FILE__, __FUNCTION__, $e->getMessage());
            }
        }// selectAll

        public static function select($tabela, $campos, $condicao = NULL){
            $sql = 'SELECT '.$campos.' FROM '.$tabela;
            if(!is_null($condicao)) $sql .= ' WHERE '.$condicao;
            try{
                $query = self::$conexao->prepare($sql);
                $query->execute();
                echo $sql;
                return $query->fetchAll(PDO::FETCH_OBJ);
            }catch(PDOException $e){
                self::erroLog(__FILE__, __FUNCTION__, $e->getMessage());
            }
        }// select

        public static function update($tabela, $dados, $condicao = NULL){
            $sql = 'UPDATE '.$tabela.' SET ';
            foreach($dados as $key => $value):
                $campos[] = $key.'=?';
                $valores[] = $value;
            endforeach;
            $sql .= implode(', ', $campos);
            if(!is_null($condicao)) $sql .= ' WHERE '.$condicao;
            try{
                $query = self::$conexao->prepare($sql);
                $query->execute($valores);
            }catch(PDOException $e){
                self::erroLog(__FILE__, __FUNCTION__, $e->getMessage());
            }
        }// update

        public static function delete($tabela, $condicao = NULL){
            $sql = 'DELETE FROM '.$tabela;
            if(!is_null($condicao)) $sql .= ' WHERE '.$condicao;
            try{
                $query = self::$conexao->prepare($sql);
                $query->execute();
            }catch(PDOException $e){
                self::erroLog(__FILE__, __FUNCTION__, $e->getMessage());
            }
        }// delete

        public static function countResult($tabela, $campo = NULL){
            !is_null($campo) ? $campo = $campo : $campo = '*';
            $sql = 'SELECT '.$campo.' FROM '.$tabela;
            try{
                $query = self::$conexao->prepare($sql);
                $query->execute();
                echo $sql;
                return count($res = $query->fetchAll(PDO::FETCH_OBJ));
            }catch(PDOException $e){
                self::erroLog(__FILE__, __FUNCTION__, $e->getMessage());
            }
        }// countResult

        public static function erroLog($arquivo, $rotina, $mensagem){
            echo 'Aconteceu um erro com os seguintes Dados:<br />';
            echo '<b>No Arquivo = </b>'.$arquivo.'<br />';
            echo '<b>Na rotina = </b>'.$rotina.'<br />';
            echo '<b>Mensagem = </b>'.$mensagem.'<br />';
        }// erroLog

    }// Banco
?>

How the class is used:

$dados = array('nome' => 'Teste', 'senha' => '123');

$dados2 = array('nome' => 'Teste2', 'senha' => '1234');

$res = Banco::select('user', 'nome');

$res = Banco::selectAll('user', "nome LIKE '%teste%'");

$res = Banco::select('user', 'nome=Teste, senha=123');
    
asked by anonymous 08.04.2015 / 01:27

1 answer

2

I would change the following points in your class:

  • Remove abstract of class: The purpose of an abstract class is to be the basis of an inherited class. In your case it is being used to prevent the bank class from being instantiated. Even though it's not being seen very well, # or some other pattern (

  • 08.04.2015 / 13:56