title: creating php class object from associative array
body: <p>I'm working on a software in which models can have custom fields. This means using a user interface, customers can add and remove fields.</p>
  
  <p>Now, I have a Customer class and I want to fill object values from associative array or JSON. Normally I would do is:</p>
  
  <pre><code>$customer = new Customer();
  
  $customer-&gt;first_name = $firstName;
  $customer-&gt;last_name = $lastName;
  .....
  </code></pre>
  
  <p>What I want is to be able to do like this:</p>
  
  <pre><code>$data = array(
    "first_name" =&gt; $firstName,
    "last_name" =&gt; $lastName,
    ....
  );
  
  $customer = getCustomer($data);
  </code></pre>
  
  <p>and the getCustomer() method should not be dependent on number of entries in the array.</p>
  
  <p>Is this doable in PHP?</p>
  
  <p>I found something like this on searching:</p>
  
  <pre><code>$customer = (object)$data;
  </code></pre>
  
  <p>Is it correct?</p>
  
  <p>Thanks</p>