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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.