How to insert the connection inside the functions without repeating the instructions?

1

Since I have several functions that will use this connection, how do I insert the $pdo into the function without copying the require and $pdo into each of the functions that I will elaborate?

require("classes/Database.php");
$pdo  = Database::connect();

function inserir (){

}
function listar (){

}
function atualizar (){

}

function excluir(){

  if ($_GET['tipo'] == 'blog' && $_GET['funcao'] == 'excluir'){

    $pdo->query("DELETE FROM tb_blog WHERE ID = " . $_GET['id'] . "");
    echo "
      <META HTTP-EQUIV=REFRESH CONTENT = '0;URL='>
      <script type=\"text/javascript\">
        window.location = \"index\";
      </script>
      ";
    return;
  }

}
excluirDados();
    
asked by anonymous 09.11.2015 / 15:24

2 answers

3

You can pass $pdo as an argument to functions or create a class to share the connection between methods. Do not inject global or external variables within a function because they can break the behavior of the function which is a piece of reusable code.

function novo ($pdo, $registro){

}
function listar ($pdo){

}
function atualizar ($pdo, $registro){

}

function excluir($pdo, $id){

    $stmt = $pdo->prepare("DELETE FROM tb_blog WHERE ID = ?");
    if(!$stmt->execute(array($id))){
       print_r($stmt->errorInfo());
    }
    echo "
      <META HTTP-EQUIV=REFRESH CONTENT = '0;URL='>
      <script type=\"text/javascript\">
        window.location = \"index\";
      </script>
      ";
    return;
  }

}


 if (!empty($_GET['tipo']) && $_GET['tipo'] == 'blog' && !empty($_GET['funcao']) && $_GET['funcao'] == 'excluir'){
    $id = !empty($_GET['id']) && ctype_digit($_GET['id'] ? $_GET['id'] : 0;
    excluir($pdo, $id);
}

Class approach, I did not repeat the validation with $_GET but it still needed, use the header() function to redirect the user it does not make sense to use javascript for this

class blogDAO{
    private $connection;

    function __construct(PDO $pdo){
        $this->connection = $pdo;
    }

    function novo($registro){

    }
    function listar(){

    }
    function atualizar ($pdo, $registro){

    }

    function excluir($id){

        $stmt = $this->connection->prepare("DELETE FROM tb_blog WHERE ID = ?");
        if(!$stmt->execute(array($id))){
            print_r($stmt->errorInfo());
            return false;
        }else{
            return true;
        }
    }

}

$daoBlog = new blogDAO(Database::connect());

if($_GET ...){
    if($daoBlog->excluir($id)){
        header('Location: index.php');
    }else{
        echo 'erro ...';
    }
}
    
09.11.2015 / 15:36
2

The idea of @rray of passing PDO as argument really is excellent.

Only as a complement, you can induce the type of the parameter to always pass PDO .

Example:

function excluir(\PDO $pdo, $id) {
    $pdo->query(/*...*/);
}

In this case, if you pass anything other than the instance of PDO , a fatal error will be generated.

This can make your code more readable to other programmers;)

Example:

$pdo = new PDO(/* Dados para conexão **/);

excluir($pdo, 5);

Or:

$ob = new MinhaClasse

$ob->excluir($pdo, 5);
    
09.11.2015 / 15:52