NeoMarine Posted January 18, 2010 Share Posted January 18, 2010 Hello, How to do this more efficiently, without using eval(); and without passing the parameters as an array into the function - I need to call the function in its usual structure: $functionName = "functionName"; $field1 = "firstFunctionParameterValue"; $field2 = "secondFunctionParameterValue"; $numberOfParameters = 2; switch ($numberOfParameters){ case "1": call_user_func($functionName, $field1); break; case "2": call_user_func($functionName, $field1, $field2); break; } So, based on "$numberOfParameters" I'd like to call_user_func() WITH that number of paramaters. Clues? Ideas? Thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/188835-call_user_function-without-knowing-how-many-parameters-to-send/ Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 in your function you can define it as such function somefunction() { } func_get_args() This will give you an array of all the values passed into the function Quote Link to comment https://forums.phpfreaks.com/topic/188835-call_user_function-without-knowing-how-many-parameters-to-send/#findComment-996906 Share on other sites More sharing options...
NeoMarine Posted January 18, 2010 Author Share Posted January 18, 2010 without passing the parameters as an array into the function [/code] I don't want to do it that way as I mentioned. No using func_get_args - for me to do that I'd have to change my functions structures site-wide which, for me is a lot of functions. Quote Link to comment https://forums.phpfreaks.com/topic/188835-call_user_function-without-knowing-how-many-parameters-to-send/#findComment-996911 Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 My post still stands... Did you READ the link? Straight from PHP.net function foo() { $numargs = func_num_args(); echo "Number of arguments: $numargs<br />\n"; if ($numargs >= 2) { echo "Second argument is: " . func_get_arg(1) . "<br />\n"; } $arg_list = func_get_args(); for ($i = 0; $i < $numargs; $i++) { echo "Argument $i is: " . $arg_list[$i] . "<br />\n"; } } foo(1, 2, 3); With the result being... Number of arguments: 3<br /> Second argument is: 2<br /> Argument 0 is: 1<br /> Argument 1 is: 2<br /> Argument 2 is: 3<br /> Quote Link to comment https://forums.phpfreaks.com/topic/188835-call_user_function-without-knowing-how-many-parameters-to-send/#findComment-996913 Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 Functions as far as I am concerned are designed to know AT least how many parameters they can take unless you use func_get_args() OR define EVERY possible parameter with a default value which is just crazy talk ie function test($var1 = '',$var2 = '',$var3 = '', etc....) Quote Link to comment https://forums.phpfreaks.com/topic/188835-call_user_function-without-knowing-how-many-parameters-to-send/#findComment-996916 Share on other sites More sharing options...
laffin Posted January 18, 2010 Share Posted January 18, 2010 Without example code, you will be given the most common answer. U want to call a variable function with a variable amount of params. However, you dont mention how u get the function name or the params. so u have gotten the same answer over and over. Quote Link to comment https://forums.phpfreaks.com/topic/188835-call_user_function-without-knowing-how-many-parameters-to-send/#findComment-996924 Share on other sites More sharing options...
NeoMarine Posted January 18, 2010 Author Share Posted January 18, 2010 Ok let me explain a bit further... In my database I store the function name and its parameters (which can be a variable amount of parameters). I then do: sizeof($Parameters) - and know from this how many parameters to put into call_user_func() - the only problem is I don't want to have to do "switch" statement based on the number of parameters... I'd like to do, if possible: call_user_func($functionName, $field1 (if field 1 exists), $field2 (if field 2 exists)) Quote Link to comment https://forums.phpfreaks.com/topic/188835-call_user_function-without-knowing-how-many-parameters-to-send/#findComment-996931 Share on other sites More sharing options...
NeoMarine Posted January 18, 2010 Author Share Posted January 18, 2010 My post still stands... Did you READ the link? Straight from PHP.net function foo() { $numargs = func_num_args(); echo "Number of arguments: $numargs<br />\n"; if ($numargs >= 2) { echo "Second argument is: " . func_get_arg(1) . "<br />\n"; } $arg_list = func_get_args(); for ($i = 0; $i < $numargs; $i++) { echo "Argument $i is: " . $arg_list[$i] . "<br />\n"; } } foo(1, 2, 3); With the result being... Number of arguments: 3<br /> Second argument is: 2<br /> Argument 0 is: 1<br /> Argument 1 is: 2<br /> Argument 2 is: 3<br /> Anyway, this would be fine but its not what I'm referring to. I haven't got a problem telling how many parameters are passed into the array. I'm just trying to call my function which is stored in a database field in the structure: functionName# var1# var2# etc... and I then explode this string by the "#" and then I know how many vars/parameters there are.. then I want to call it using call_user_func() - but to do so without using eval to "assemble" the function string to be executed Quote Link to comment https://forums.phpfreaks.com/topic/188835-call_user_function-without-knowing-how-many-parameters-to-send/#findComment-996939 Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 I understand your problem better now.. Why dont you use call_user_func_array() Im sure thats what your after. Quote Link to comment https://forums.phpfreaks.com/topic/188835-call_user_function-without-knowing-how-many-parameters-to-send/#findComment-996942 Share on other sites More sharing options...
NeoMarine Posted January 18, 2010 Author Share Posted January 18, 2010 Wow I didn't know of that array version of the function. Many thanks! It works Quote Link to comment https://forums.phpfreaks.com/topic/188835-call_user_function-without-knowing-how-many-parameters-to-send/#findComment-996944 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.