PHP function with unlimited number of parameters

25,480

Solution 1

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.

Solution 2

Have you taken a look at func_get_args, func_get_arg and func_num_args

So for example:

function foo(){
    if ( func_num_args() > 0 ){
        var_dump(func_get_args());
    }
}

or:

function bind_param(){
    if ( func_num_args() <= 1 ){
        return false; //not enough args
    }
    $format = func_get_arg(0)
    $args = array_slice(func_get_args(), 1)

    //etc
}

EDIT
Regarding Ewan Todds comment:
I don't have any knowlege of the base API you are creating the wrapper for, but another alternative may be to do something with chaining functions so your resulting interface looks something like:

$var->bind()->bindString($code)
            ->bindString($language)
            ->bindString($official)
            ->bindDecimal($percent);

Which I would prefer over the use of func_get_args as the code is probably more readable and more importantly less likely to cause errors due to the the lack of a format variable.

Solution 3

Previously you should have used func_get_args(), but in the php 5.6, you can use ... operator.

So for example you can write the function which returns you a sum of all the numbers, sent to the function:

function bind_param(...$params){
   var_dump($params);
}

Your params is actually an array of all elements.

Solution 4

If you are using PHP 5.6 or later version, argument lists may include the ... token to denote that the function accepts a variable number of arguments. The arguments will be passed into the given variable as an array; Simply Using ... you can access unlimited variable arguments.

for example:

<?php
function sum(...$numbers) {
    $sum = 0;
    foreach ($numbers as $n) {
        $sum += $n;
    }
    return $sum ;
}

echo sum(1, 2, 3, 4, 5);
?>

If you are using PHP version <= 5.5 then you can access unlimited parameterusing the func_num_args(), func_get_arg(), and func_get_args() functions.

For example:

<?php
function foo()
{
    $numargs = func_num_args();
    echo "Number of arguments: $numargs \n";
    if ($numargs >= 2) {
        echo "Second argument is: " . func_get_arg(1) . "\n";
    }
    $arg_list = func_get_args();
    for ($i = 0; $i < $numargs; $i++) {
        echo "Argument $i is: " . $arg_list[$i] . "\n";
    }
}

foo(1, 2, 3);
?>

Solution 5

Use func_get_args():

function my_func() {
  $args = func_get_args();
  foreach ($args as $arg) {
    echo "Arg: $arg\n";
  }
}
Share:
25,480

Related videos on Youtube

Ageis
Author by

Ageis

Updated on May 14, 2020

Comments

  • Ageis
    Ageis about 4 years

    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);
    
    • Your Common Sense
      Your Common Sense over 11 years
      you can't do it without toilsome efforts. I'd suggest either build a query usual way, or move to PDO if you want native prepared statements at any cost. They have bindValue, mind you
    • E.T.
      E.T. over 11 years
      Thanks for the info. Unfortunately PDO appears to be overkill, with even more effort and boilerplate, compared to just writing my own simple stuff.
    • Your Common Sense
      Your Common Sense over 11 years
      For the simple stuff you can use a lib from my userinfo. It's real simple though.
  • Ewan Todd
    Ewan Todd over 14 years
    I use func_get_args from time to time. It suffers from making it difficult see at a glance what the method's inputs are. Plus, it introduces a block of butt ugly syntax into your code. I would recommend this approach only as a last resort. Prefer the Parameter Object refactoring.
  • Yacoby
    Yacoby over 14 years
    I agree with you that it is a bad way of doing variable arguments. I have added an alternative idea to my answer
  • Ewan Todd
    Ewan Todd over 14 years
    I like the "chaining functions" proposal better than func_get_args. I know this design as a FLUENT interface. It is accomplished by having bindString(), bindDecimal() etc. do "return $this;" as their last line. The FLUENT interface is a great option for cetain types of factory.
  • Josh Davis
    Josh Davis over 14 years
    Indeed. Last time I checked (1+ year ago) there was no way to wrap/extend bind_param() because func_get_args() won't return references.
  • Danack
    Danack over 11 years
    This doesn't work if the parameters are not set, and are then passed by reference, to be set inside the function. func_get_args tries to access the unset variable which throws an error.