Page 1 of 1
Do I need to use reflection, if so, how?!
Posted: Wed Feb 04, 2009 4:49 pm
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
Thanks!
Re: Do I need to use relection, if so, how?!
Posted: Wed Feb 04, 2009 5:13 pm
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?
Re: Do I need to use reflection, if so, how?!
Posted: Wed Feb 04, 2009 5:41 pm
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.
Re: Do I need to use reflection, if so, how?!
Posted: Wed Feb 04, 2009 7:54 pm
by Chris Corbyn
call_user_func_array().
Re: Do I need to use reflection, if so, how?!
Posted: Wed Feb 04, 2009 8:24 pm
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
Re: Do I need to use reflection, if so, how?!
Posted: Wed Feb 04, 2009 9:03 pm
by Chris Corbyn
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);
Re: Do I need to use reflection, if so, how?!
Posted: Wed Feb 04, 2009 9:52 pm
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.
Re: Do I need to use reflection, if so, how?!
Posted: Thu Feb 05, 2009 1:19 am
by Inkyskin
Excellent guy's, thanks

I'll check it in a bit and see if it's all working!
Re: Do I need to use reflection, if so, how?!
Posted: Thu Feb 05, 2009 12:00 pm
by Inkyskin
Using call_user_func_array has worked perfectly, thanks all!