title: How to use mysqli::fetch_object()?
body: <p>How do people usually turn mysqli results into objects?</p>
  
  <p>I cannot find any examples with fetch_object() when using custom classes. Assume the following setup</p>
  
  <pre><code>class Test1 {
      function __construct(array $data) { ... }
  }
  
  class Test2 {
      function __construct(array $data) { ... }
  }
  </code></pre>
  
  <p>Assume that I have multiple classes with the same constructors, how can I dynamically instantiate these classes?</p>
  
  <p>I would imagine something like this:</p>
  
  <pre><code>function customQuery($query, $class) {
  
      // Get some data by mysqli query, assume we get the data as $result object
      ...
  
      // Now I would like to store the data in an array with instances of $class
      $array = array();
      while ($obj = $result-&gt;fetch_object($class, ???) {
          $array[] = $obj;
      }
  
      // Return the array with instances of $class
      return $array;
  
  }
  </code></pre>
  
  <p>What do I use as arguments there for my question marks? I only know that both class constructors of Test1 and Test2 want an associative array as input (probably not the same length!).</p>
  
  <p>In the end I simply want to do something like</p>
  
  <pre><code>$arr1 = customQuery('SELECT id, product FROM test1 LIMIT 10', 'Test1');
  $arr2 = customQuery('SELECT id, name, address FROM test2 LIMIT 10', 'Test2');
  </code></pre>
  
  <p>Of course I would appreciate your input if you have better ideas to achieve my goal.</p>