Do I need to use reflection, if so, how?!

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Do I need to use reflection, if so, how?!

Post by Inkyskin »

Hey all,

I'm stuck! I am calling a class, and a method within it dynamically. Normally this would be straight forward, however I also need to pass some arguments to the method, this is what stumps me.

Say for example I did:

Code: Select all

$args = array(
  '1' => 'var1',
  '2' => 'var2'
);
 
$obj = new $class;
$obj->$method($args);
But my method takes 2 arguments:

Code: Select all

method($arg1, $arg2){
  return true;
}
How can I pass 2 arguments. If you use an array like I did above, it only counts as one argument... By the way, these array's are created dynamically too, so no real way of providing myself two (or however many needed) variables.

Am I missing something obvious? I've read up on reflection, but to be honest, it's confused the hell out of me :banghead:

Thanks!
Last edited by pickle on Wed Feb 04, 2009 5:16 pm, edited 1 time in total.
Reason: Fixed spelling error in subject
mickeyunderscore
Forum Contributor
Posts: 129
Joined: Sat Jan 31, 2009 9:00 am
Location: UK

Re: Do I need to use relection, if so, how?!

Post by mickeyunderscore »

Well you could pass each index separately:

Code: Select all

$args = array(
  '1' => 'var1',
  '2' => 'var2'
);
 
$obj = new $class;
$obj->$method($args[0], $args[1]);
If that's what you need?
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: Do I need to use reflection, if so, how?!

Post by Inkyskin »

Hi, thanks, but unfortunately, the code never actually know whether it needs to pass 2, 4 or more variables, it's always going to be dynamic. It's just going to be an array that matches the amount of arguments needed, whatever the number may be.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Do I need to use reflection, if so, how?!

Post by Chris Corbyn »

call_user_func_array().
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Do I need to use reflection, if so, how?!

Post by Eran »

Use func_get_args() inside the method to get all the passed parameters - http://www.php.net/manual/en/function.func-get-args.php
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Do I need to use reflection, if so, how?!

Post by Chris Corbyn »

pytrin wrote:Use func_get_args() inside the method to get all the passed parameters - http://www.php.net/manual/en/function.func-get-args.php
That won't help in this case. It would help if Inkyskin was trying to do the opposite and retrieve a variable number of arguments however. What is needed in this case though is a method invocation with a variable number of arguments so we're looking at it from the outside, not the inside.

You can do it with Reflection or with call_user_func_array().

The Reflection way (might be slower, and certainly isn't very nice to read):

Code: Select all

$args = array(
  '1' => 'var1',
  '2' => 'var2'
);
 
$reflector = new ReflectionClass($class);
 
$obj = $reflector->newInstance();
 
$method = $reflector->getMethod($method);
 
$method->invokeArgs($obj, $args);
The call_user_func_array() way:

Code: Select all

$args = array(
  '1' => 'var1',
  '2' => 'var2'
);
 
$obj = new $class;
call_user_func_array(array($obj, $method), $args);
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Do I need to use reflection, if so, how?!

Post by alex.barylski »

^^^

What he said...call_user_func_array definetely not best solved using reflection...that would be like using eval() instead of include...wrong tool for the job despite being possible.
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: Do I need to use reflection, if so, how?!

Post by Inkyskin »

Excellent guy's, thanks :D I'll check it in a bit and see if it's all working!
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: Do I need to use reflection, if so, how?!

Post by Inkyskin »

Using call_user_func_array has worked perfectly, thanks all!
Post Reply