title: Argument packing and unpacking when instantiating a new class instance in PHP
body: <p>In Python, you can pack and unpack arguments like so:</p>
  
  <pre><code>def myfunction(arg1, arg2, arg3):
      pass
  
  mydict = dict(arg1='foo', arg2='bar', arg3='baz')
  
  myfunction(**mydict)
  
  mylist = ['foo', 'bar', 'baz']
  
  myfunction(*mylist)
  </code></pre>
  
  <p>Now, is there something similar in PHP? I have a class that is passed to a function, along with an associative array extracted from a database record. I want to create an instance of the class, which accepts arguments that are named the same as the keys in the array. Can it be done?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Ok, so I definitely didn't formulate my question correctly. I wanted to instantiate a new class dynamically when I know neither the class name, nor the number of arguments beforehand.</p>
  
  <p>I've found the Answer though:</p>
  
  <p><a href="http://php.net/manual/en/book.reflection.php" rel="nofollow">http://php.net/manual/en/book.reflection.php</a></p>
  
  <p>Here's the solution:</p>
  
  <pre><code>class MyClass {
      public function __construct($var1, $var2, $var3) {
          echo "$var1\n";
          echo "$var2\n";
          echo "$var3\n";
      }
  }
  
  $reflector = new ReflectionClass('MyClass');
  $args = array('var1'=&gt;'foo', 'var2'=&gt;'baz', 'var3'=&gt;'bar');
  $instance = $reflector-&gt;newInstanceArgs($args);
  </code></pre>