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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.