ShoeLace1291 Posted September 24, 2012 Share Posted September 24, 2012 I need to write a function that calls another functions, but it needs to pass arguements to the new function with an array... Here's an example: echo fruit('colors', array('banana' => 'yellow', 'apple' => 'red', 'pear' => 'green'); Would basically translate to echo fruit($method = 'colors', $banana = 'yellow', $apple = 'red', $pear' = 'green'); I need to do this because the arguements for the functions that are being called are usually different. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/268726-define-function-arguements-with-string/ Share on other sites More sharing options...
gizmola Posted September 24, 2012 Share Posted September 24, 2012 You can pass whatever you want as a parameter to a function. That includes objects, arrays, or what have you. Quote Link to comment https://forums.phpfreaks.com/topic/268726-define-function-arguements-with-string/#findComment-1380489 Share on other sites More sharing options...
requinix Posted September 24, 2012 Share Posted September 24, 2012 If you're saying that $method, $banana, $apple, and $pear are parameters of fruit() like function fruit($method, $banana, $apple, $pear) then you'll need reflection. What matters when calling a function is the order of the arguments, not what parameters you're trying to fill. Use reflection to grab the list of parameters of the function and, in order, build up an array of the arguments you have. Be wary of optional parameters and type hinting. Once you've got your array you can use call_user_func_array. Quote Link to comment https://forums.phpfreaks.com/topic/268726-define-function-arguements-with-string/#findComment-1380516 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.