title: Prepared statements and class inheritance in PHP
body: <p>I have a problem when doing a prepared statement in PHP. My main goal is to create two classes, the first one for connecting (Conexion) to the server and the database and the second one to do prepared statements (RealizarConsultas).</p>
  
  <p>I have decided that the class used to do statements should inherit from "Conexion". By doing this I pretend to connect with the database each time I use the function "RealizarConsultas::MostrarConsulta()". This function should connect with the database, then it should do the prepared statement and finally it should close the connection.</p>
  
  <p>Class used for connection.</p>
  
  <pre><code>&lt;?php
  
      class Conexion extends PDO{
  
          protected $conexionDb;
  
          public function Conexion(){ 
              try{
                  $conexionDb=new PDO ('mysql:host=localhost;dbname=euroburo','root','');
                  $conexionDb-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                  $conexionDb-&gt;exec("SET CHARACTER SET utf8");
  
  
  
              }catch(Exception $e){
  
                  echo "Error: ".$e-&gt;getLine();
  
              }
          }
          public function stopConexion(){
              $conexionDb-&gt;close();
          }
  
      }
  ?&gt;
  </code></pre>
  
  <p>Class used to do prepare statements.</p>
  
  <pre><code>&lt;?php
      require("conexion.php");
      class RealizarConsulta extends Conexion{
          public function RealizarConsulta(){
              parent::__construct();
          }
          public function MostrarConsulta($consulta){
              $resultado=$this-&gt;conexionDb-&gt;prepare($consulta);
              $resultado-&gt;execute();
              $final = $resultado-&gt;fetch(PDO::FETCH_ASSOC);  
              print_r($final);
              $this-&gt;stopConexion();
          }
      }
  
  ?&gt;
  </code></pre>
  
  <p>My main problem is in this line, located inside "RealizarConsulta::MostrarConsulta()".</p>
  
  <pre><code>$resultado=$this-&gt;conexionDb-&gt;prepare($consulta);
  </code></pre>
  
  <p>Each time I call to the function (RealizarConsulta::MostrarConsulta());</p>
  
  <p>This error appears: Call to a member function prepare() on a non-object.</p>
  
  <p>I understand that this error tells me that $this->conexionDb is not an object but I don't really understand why. The reason I don't understand the error is due to the fact that $conexionDb is a protected variable created in the parent class, as a protected variable "RealizarConsulta" class should inherit it without any problem and I should have access to it. $conexionDb as you can see is initialised when the constuctor is called, and it should hold the information related to the connection. </p>
  
  <p>As you can see through my reasoning, $conexionDb should be a reference to an object that belongs to "Conexion" class and I should have access to it in "RealizarConsulta" class. </p>
  
  <p>I would like to know in what I am failing and how can I resolve the error. Thanks</p>