Jump to content

Define Function Arguements With String?


ShoeLace1291

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/268726-define-function-arguements-with-string/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.