title: PHP: How to instantiate a class with arguments from within another class
body: <p>I am in a situations where i need to instantiate a class with arguments from within an instance of another class.
  Here is the prototype:</p>
  
  <pre><code>//test.php
  
  class test
  {
      function __construct($a, $b, $c)
      {
          echo $a . '&lt;br /&gt;';
          echo $b . '&lt;br /&gt;';
          echo $c . '&lt;br /&gt;';
      }
  }
  </code></pre>
  
  <p>Now, i need to instantiate above class using below class's <strong>cls</strong> function:</p>
  
  <pre><code>class myclass
  {
  function cls($file_name, $args = array())
  {
      include $file_name . ".php";
  
      if (isset($args))
      {
          // this is where the problem might be, i need to pass as many arguments as test class has.
          $class_instance = new $file_name($args);
      }
      else
      {
          $class_instance = new $file_name();
      }
  
      return $class_instance;
  }
  }
  </code></pre>
  
  <p>Now when i try to create an instance of test class while passing arguments to it:</p>
  
  <pre><code>$myclass = new myclass;
  $test = $myclass-&gt;cls('test', array('a1', 'b2', 'c3'));
  </code></pre>
  
  <p>It gives error:
  Missing argument 1 and 2; only first argument is passed.</p>
  
  <p>This works fine if i instantiate a class which has no arguments in it's constructor function.</p>
  
  <p>For experienced PHP developers, above should not be much of a problem. Please help.</p>
  
  <p>Thanks</p>