5

I will explain the question with a simple function accepting any number of function

function abc() {
   $args = func_get_args();
   //Now lets use the first parameter in something...... In this case a simple echo
   echo $args[0];
   //Lets remove this first parameter 
   unset($args[0]); 

   //Now I want to send the remaining arguments to different function, in the same way as it received
   .. . ...... BUT NO IDEA HOW TO . ..................

   //tried doing something like this, for a work around
   $newargs = implode(",", $args); 
   //Call Another Function
   anotherFUnction($newargs); //This function is however a constructor function of a class
   // ^ This is regarded as one arguments, not mutliple arguments....

}

I hope the question is clear now, what is the work around for this situation?

Update

I forgot to mention that the next function I am calling is a constructor class of another class. Something like

$newclass = new class($newarguments);
1
  • 1
    Sending the remaining arguments as an array is not an acceptable solution. I want to send the argument as the same way as "arg[1], arg[2].. so on and so forth... Commented Jun 21, 2011 at 7:57

1 Answer 1

12

for simple function calls

use call_user_func_array, but do not implode the args, just pass the array of remaining args to call_user_func_array

call_user_func_array('anotherFunction', $args);

for object creation

use: ReflectionClass::newInstanceArgs

$refClass = new ReflectionClass('yourClassName');
$obj = $refClass->newInstanceArgs($yourConstructorArgs);

or: ReflectionClass::newinstance

$refClass = new ReflectionClass('yourClassName');
$obj = call_user_func_array(array($refClass, 'newInstance'), $yourConstructorArgs);
Sign up to request clarification or add additional context in comments.

4 Comments

What if the next function is a constructor of a class?
@Starx See the added example.
For the object creation, which one is preferred solution, first one or the second
@Starx I'd go for the first one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.