Re: Variable number of parameters

From: Date: Tue, 05 Aug 2008 22:07:09 +0000
Subject: Re: Variable number of parameters
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-277751@lists.php.net to get a copy of this message
Philip Thompson wrote:
Is it possible to grab a variable number of parameters and send the appropriate amount to another function? <?php // Some class $this->db->prepare("SELECT * FROM table WHERE (id=?)"); $this->db->bind('ii', $id1); $this->db->prepare("SELECT * FROM table WHERE (id=? AND other_id=?)"); $this->db->bind('ii', $id1, $id2); // DB class function bind () { $args = func_get_args(); $this->statement->bind_param($args[0], $args[1], ...); } ?> Ok, is it possible to send any number of variables to db->bind() in order to send those to statement->bind_param()? Or, if someone else has a better db abstraction method, feel free to educate... Thanks, ~Phil
<?php ##### WARNING #### # The following example uses eval() to do the execution. # The following answer is a YMMV answer. # Be careful with it. I could be a sleeping snake waiting to bite... ##### function myFunction () { // Get your list of values passed in $values = func_get_args(); // try and secure your data a little foreach ( $values AS $k => $v ) {
    // Check to see if this value is a string, if so escape the data
    // Else, let is pass through untouched!
    if ( is_string($v) ) {
      $values[$k] = escapeshellarg($v);
    } else {
      $values[$k] = $v;
    }
} // Put it all together in one long string $arguments = join(', ', $values); // Combined your argument string and function call // Process and return results. return eval("return anotherFunction({$arguments})"); } myFunction (1, 5, 'Hi'); // This will result in calling anotherFunction as such anotherFunction ('1, 5, Hi'); // and not anotherFunction (1, 5, 'Hi'); ?> If this isn't what you are looking for, please explain a little more! -- Jim Lucas "Some men are born to greatness, some achieve greatness, and some have greatness thrust upon them." Twelfth Night, Act II, Scene V by William Shakespeare

« previous php.general (#277751) next »