title: PHP - Fetching an Object from a Custom Class using PDO
body: <p>So I am new to PHP and particularly the world of PDO.
  I am using PDO to select some data from a table and create an object out of this data. I have been successful with PDO creating a default object and accessing the results. This is the fetch statement for fetching a default object, which works as expected:</p>
  
  <pre><code>$stmt-&gt;execute();
  $result = $stmt-&gt;fetch(PDO::FETCH_OBJ);
  </code></pre>
  
  <p>The next thing I want to do is fetch the results and create a custom object out of a predefined class that I have created. I based the below statement on what I was using for the first statement, however, this does not work</p>
  
  <pre><code>$stmt-&gt;execute();
  $user_object = $stmt-&gt;fetch(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'classname');
  </code></pre>
  
  <p>I have got it working by setting the fetch mode outside of the fetch method and can be seen here:</p>
  
  <pre><code>$stmt-&gt;execute();
  $stmt-&gt;setFetchMode(PDO::FETCH_CLASS| PDO::FETCH_PROPS_LATE,'classname');
  $object = $stmt-&gt;fetch();
  </code></pre>
  
  <p>I have consulted the PHP manuals but I do not understand why I cannot have a statement like the second one i.e. set the fetchmode within the fetch statement.</p>
  
  <p>Thanks</p>
  
  <p><strong>Update</strong></p>
  
  <p>The following statement: </p>
  
  <pre><code>$user_object = $stmt-&gt;fetch(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'classname');
  var_dump($stmt-&gt;errorInfo());
  var_dump($user_object);
  </code></pre>
  
  <p>Returns -</p>
  
  <pre><code>array(3) { [0]=&gt; string(5) "00000" [1]=&gt; NULL [2]=&gt; NULL } bool(false)
  </code></pre>