Jump to content

call_user_function - without knowing how many parameters to send


NeoMarine

Recommended Posts

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?

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.

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

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

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.

 

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

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

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.