title: PHP, PDO, last query, bindParam
body: <p>I would like to have the last query (for debugging purposes) from the PDOStatement. But I can't override the bindParam and bindValue methods. When I tried the code below, I got:</p>
  
  <blockquote>
    <p>Fatal error: Default value for parameters with a class type hint can only be NULL</p>
  </blockquote>
  
  <p>Then I replaced the <code>PDO::PARAM_STR</code> to <code>null</code> in the parameter list of bindParam/bindValue, so I got:</p>
  
  <blockquote>
    <p>Strict Standards: Declaration of DBStatement::bindParam() should be compatible with that of PDOStatement::bindParam()</p>
  </blockquote>
  
  <p>I then deleted <code>int</code> before the <code>$data_type</code> parameter and set the default value to <code>PDO::PARAM_STR</code>. Then I got:</p>
  
  <blockquote>
    <p>Strict Standards: Declaration of DBStatement::bindParam() should be compatible with that of PDOStatement::bindParam() in D:\www\pdotest.php on line 73</p>
  </blockquote>
  
  <p>(interestingly, the bindValue is okay now).</p>
  
  <p>So, what can I do now?</p>
  
  <pre><code>class DBConnection extends PDO
  {
      public function __construct($dsn, $username = null, $password = null, $driver_options = array())
      {
          parent::__construct($dsn, $username, $password, $driver_options);
  
          $this-&gt;setAttribute(PDO::ATTR_STATEMENT_CLASS, array('DBStatement', array($this)));
          $this-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      }
  }
  
  class DBException extends PDOException
  {
      private $query_string;
      private $parameters;
  
      public function __construct($message = '', $code = 0, $previous = null, $query_string = '', $parameters = array())
      {
          parent::__construct($message, $code, $previous);        
  
          $this-&gt;query_string = $query_string;
          $this-&gt;parameters = $parameters;
      }
  
      public function getQueryString()
      {
          return $this-&gt;query_string;
      }   
  
      public function getParameters()
      {
          return $this-&gt;parameters;
      }
  }
  
  class DBStatement extends PDOStatement
  {
      private $conn;
      private $parameters = array();
  
      protected function __construct($conn)
      {
          $this-&gt;conn = $conn;
      }
  
      public function bindParam($parameter, &amp;$variable, int $data_type = PDO::PARAM_STR, int $length = null, $driver_options = null)
      {
          $this-&gt;parameters[$parameter] = $variable;
  
          parent::bindParam($parameter, $variable, $data_type, $length, $driver_options);
      }
  
      public function bindValue($parameter, $value, int $data_type = PDO::PARAM_STR)
      {
          $this-&gt;parameters[$parameter] = $value;
  
          parent::bindValue($parameter, $value, $data_type);
      }
  
      public function execute($input_parameters = null)
      {
          try
          {
              parent::execute($input_parameters);
          }
          catch (PDOException $e)
          {
              throw new DBException($e-&gt;getMessage(), $e-&gt;getCode(), $e-&gt;getPrevious(), $this-&gt;queryString, $this-&gt;parameters);
          }
      }
  }
  
  $id = 1;
  
  try
  {
      $conn = new DBConnection('mysql:host=localhost;dbname=test', 'root', '');
      $stmt = $conn-&gt;prepare('select * from foo where id = :id');
      $stmt-&gt;bindParam(':id', $id);
      $stmt-&gt;execute();
  }
  catch (DBException $e)
  {
      echo "Query string was: ".$e-&gt;getQueryString()."\n";
      echo "Parameters was: ".print_r($e-&gt;getParameters(), true);
  }
  </code></pre>
  
  <p>I also recieve te following when I throw a DBException (because of $code parameter):</p>
  
  <blockquote>
    <p>Notice: A non well formed numeric value encountered in D:\www\pdotest.php on line 21 </p>
  </blockquote>