Stooney Posted October 28, 2008 Share Posted October 28, 2008 Would it be possible to do something like this? I hope it's self explanatory enough. <?php class Foo{ public function bar($var1, $var2){ return $var1 + $var2; } } $class='Foo'; $action='bar'; $args=array('var1'=>'5', 'var2'=>'3'); $total=$class->$action($args); Quote Link to comment https://forums.phpfreaks.com/topic/130498-solved-dynamic-method-args/ Share on other sites More sharing options...
corbin Posted October 29, 2008 Share Posted October 29, 2008 public function bar($arr) { //do something with array of functions } Quote Link to comment https://forums.phpfreaks.com/topic/130498-solved-dynamic-method-args/#findComment-677087 Share on other sites More sharing options...
Stooney Posted October 29, 2008 Author Share Posted October 29, 2008 I was in a hurry to finish that post earlier, here's an actual example I'm looking at: In my registry class I have a method called useObject. Basically that just allows me to use objects stored in the registry (which only have private access) without having to pull it out of the registry. $registry->useObject('template', 'load', '404'); In that bit, template is an object stored in the registry. 'load' is a method, and '404' is what I'm passing as an argument. That all works fine. But what if the load method needed 3 args? My registry's useObject method needs to be able to call different methods that take various amounts of args, so I can't just send an array of args unless each method was written to accept them. I basically need to somehow do this type of 'translation': $registry->useObject('template', 'load', 'var1', 'var2', 'var3'); OR $registry->useObject('template', 'load', array('var1', 'var2', 'var3')); Which should be equivalent to $template->load('var1', 'var2', 'var3'); Does that make any more sense? If it isn't possible, what would you suggest as far as a different approach to my useObject method? Just popped in my head: Is it 'better' if all functions/methods just took an array of args? I guess that could technically work...like instead of: function($var1, $var2){ return $var1+$var2; } something like function($args){ return $args['var1']+$args['var2']; } Quote Link to comment https://forums.phpfreaks.com/topic/130498-solved-dynamic-method-args/#findComment-677106 Share on other sites More sharing options...
trq Posted October 29, 2008 Share Posted October 29, 2008 Still not sure I understand your question but this may help. A function can except an ininant amount of arguments if you don't bother declaring them in your definition. eg; <?php function foo() { foreach (func_get_args() as $arg) { echo $arg . "\n"; } } foo(1,2,3,4,5,6,7,8,9,'bob',4,5,6,'blah','blah','boo!'); Quote Link to comment https://forums.phpfreaks.com/topic/130498-solved-dynamic-method-args/#findComment-677117 Share on other sites More sharing options...
Daniel0 Posted October 29, 2008 Share Posted October 29, 2008 I'm guessing he wants to do like you can do in Python. If that's the case then no, it's not possible. Quote Link to comment https://forums.phpfreaks.com/topic/130498-solved-dynamic-method-args/#findComment-677684 Share on other sites More sharing options...
DarkWater Posted October 29, 2008 Share Posted October 29, 2008 Check out call_user_func_array(). Quick example: <?php function do_call($object, $method, $args) { call_user_func_array(array($object, $method), $args); } class Foo { public function test($var, $var2) { echo $var . $var2; } } $foo = new Foo; do_call($foo, 'test', array('Hello,', ' World')); Except you'd use an already existing object. Quote Link to comment https://forums.phpfreaks.com/topic/130498-solved-dynamic-method-args/#findComment-677905 Share on other sites More sharing options...
Daniel0 Posted October 29, 2008 Share Posted October 29, 2008 Reinventing deprecated functions, eh? http://dk.php.net/manual/en/function.call-user-method-array.php Quote Link to comment https://forums.phpfreaks.com/topic/130498-solved-dynamic-method-args/#findComment-677954 Share on other sites More sharing options...
DarkWater Posted October 29, 2008 Share Posted October 29, 2008 Essentially. But I'm probably going to have to go out on a limb and say that the function sucked (implementation-wise) back in PHP4 because of the object model. And since call_user_func_array() supports method calls, it's probably the new, 'correct' replacement. Quote Link to comment https://forums.phpfreaks.com/topic/130498-solved-dynamic-method-args/#findComment-677956 Share on other sites More sharing options...
Stooney Posted October 30, 2008 Author Share Posted October 30, 2008 Thank you guys very much. I saw you mentioned call_user_func_array() back in my other post but until now didn't fully understand it. Quote Link to comment https://forums.phpfreaks.com/topic/130498-solved-dynamic-method-args/#findComment-678188 Share on other sites More sharing options...
DarkWater Posted October 30, 2008 Share Posted October 30, 2008 Yup. Quote Link to comment https://forums.phpfreaks.com/topic/130498-solved-dynamic-method-args/#findComment-678307 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.