Undefined Variable using this

0

I am creating a basic class for operations in the database, but apache returns me two errors on line 12. Because the connection is an attribute I refer to this . Why in the first reference at the time of connecting to db the error is not given?

Errors:

Notice: Undefined variable: conexao in C:\xampp\htdocs\MinhasFuncoes\php\model.php on line 12

Fatal error: Cannot access empty property in C:\xampp\htdocs\MinhasFuncoes\php\model.php on line 12

View.php:

require_once 'model.php';
 $conn = new model("mysql","localhost", "teocratico", "utf-8","root","");
 $conn->consulta("descricao","desafios");

Model.php:

<?php
class model {
  private $conexao;
  public function __construct ($db, $host, $dbname, $charset, $usuario, $senha){
    try{
      $this->$conexao = new PDO ("$db:host=$host; dbname=$dbname; charset=$charset","$usuario","$senha");
    } catch (PDOException $erro){
        return $erro->getmessage();
    }
  }
  public function consulta ($campos, $tabela){
    $this->$conexao->prepare("SELECT :campos FROM :tabela;"); //Erro
    $this->$conexao->BindParam(':campos', $campos);
    $this->$conexao->BindParam(':tabela', $tabela);
    $this->$conexao->execute();
    $resultado = $this->$conexao->fetchAll(PDO::FETCH_ASSOC);
    echo $resultado;
  }
}
?>
    
asked by anonymous 23.05.2017 / 21:08

1 answer

2

Your logic is right, however, there should not be 2 $ .

Change everywhere $this->$conexao by $this->conexao

    
23.05.2017 / 21:12