Alex Rivera | Logout

PHP function with unlimited number of parameters

Asked 2009-10-16T10:47:21.990
25

How do I write a function that can accept unlimited number of parameters?

What am trying to do is create a function within a class that wraps the following:

$stmt->bind_param('sssd', $code, $language, $official, $percent);
Edit
Report

2 Answers

5

Use func_get_args():

function my_func() {
  $args = func_get_args();
  foreach ($args as $arg) {
    echo "Arg: $arg\n";
  }
}
answered 2009-10-16T10:50:18.423
4

The above suggests are all good, but I don't think they will be suitable for your situation.

$stmt->bind_param('sssd', $code, $language, $official, $percent);

If you want to wrap this function, you will need to pass references to the original argument variables to the bind_param function. I don't think func_get_args() gives you this, it gives you values instead. Thus it won't be possible to use these to pass to the parent function. I battled with a similar issue when trying to extend mysqli_stmt and never came to satisfactory solution.

This is not really an answer to your question I'm afraid, just a warning that other arguments may not work in your particular application of arbitrary number of arguments.

answered 2009-10-16T11:04:55.420

Your Answer