title: PHP dynamic class loading
body: <p>Lets say that I have an array that I want to convert to a value object.</p>
  
  <p>My value object class is as follows:</p>
  
  <pre><code>/* file UserVO.php*/
   class UserVO
   {
      public $id;
      public $email;
  
       public function __construct($data)
       {
           $this-&gt;id = (int)$data['id'];
           $this-&gt;email = $data['email'];
       } 
   }
  </code></pre>
  
  <p>And I create my array of value objects as follows:</p>
  
  <pre><code>/* file UserService.php*/
  $array = array(
  array(...),
  array(...));
  $count = count($array);
  for ($i = 0; $i &lt; $count; $i++)
  {
     $result[] = new UserVO($array[$i]);
  }
  return $result;
  </code></pre>
  
  <p>OK, so this all works fine. However, I'd like to specificy the VO that is to be created dynamically, so that I can have a single dynamic function to create my VO's.</p>
  
  <p>Something like:</p>
  
  <pre><code>$ret = create_vo($array, 'UserVO');
  
  function create_vo($data, $vo)
  {
    $count = count($data);
    for ($i = 0; $i &lt; $count; $i++)
    {
       $result[] = new $vo($data[$i]); //this obviously wont work...Class name must be a valid object or a string
    }
    return $result;
  }
  </code></pre>
  
  <p>I realise that I could do this with a switch statement (iterating through all my VO's)...but there is no doubt a much much more elegant solution. It would also be supercool if I could lazy load the VO's as needed, instead of having multiple 'includes'</p>
  
  <p>Any help much appreciated.</p>