title: Setting and accessing properties of a PHP class object where the values have been retrieved from a DB within the class
body: <p>I've created a simple class that aims to retrieve details for staff members from a MySQL DB. Here is the class:</p>
  
  <pre><code>class staff {
      public function __construct($db, $id){
          $record = $db-&gt;prepare("SELECT * FROM staff WHERE id = :id");
          $record-&gt;execute(array(':id' =&gt; $id));
          foreach($record as $k=&gt;$v){
              $this-&gt;$k = $v;
          }
      }
  }
  </code></pre>
  
  <p>And here is the call:</p>
  
  <pre><code>$staffMember = new staff($db, $uid);
  var_dump($staffMember);
  </code></pre>
  
  <p>And the var_dump gives this:</p>
  
  <pre><code>object(staff)#7 (1) {
    ["0"]=&gt;
    array(2) {
      ["fname"]=&gt;
      string(4) "Mike"
      [0]=&gt;
      string(4) "Mike"
    }
  }
  </code></pre>
  
  <p>So far so good, I can see things are retrieved and set. But I'm not happy about the implementation, two reasons:</p>
  
  <p>1) It seems that values are duplicated, as I have two "Mike"'s in the array that is returned. Why is this happening and how do I prevent it?</p>
  
  <p>2) Accessing the values is not straightforward, in the main PHP file I use something along the lines of:</p>
  
  <pre><code>$staffMember-&gt;{0}["fname"];
  </code></pre>
  
  <p>to retrieve the value of a staff members name. This doesn't look right, I expected to use $staffMember->fname. Is there another approach that will result in retrieved DB values being set as class/object variables rather than a single property that is actually an array and is rather cumbersome to access and probably even worse to manipulate?</p>
  
  <p>My thanks in advance.</p>