Re: Variable number of parameters

From: Date: Wed, 06 Aug 2008 21:25:07 +0000
Subject: Re: Variable number of parameters
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-277780@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
I'm confused as your code looks like it's already doing what you're asking. It's hard to tell without seeing what bind_param() looks like. But just a thought to use arrays in one of two ways: // 1. $this->db->bind('ii', $id1, $id2); function bind () { $args = func_get_args(); $this->statement->bind_param($args); // then bind_param() can count the number of args and use them } //2. $this->db->bind('ii', array($id1, $id2)); function bind ($var, $ids) { // pass thru to bind_param() $this->statement->bind_param($var, $ids); // then bind_param() can use the $ids array // or count the ids and send individual args to bind_param() } As I said, it's kind of hard to tell without knowing exactly what you want to achieve. One of the more elegant ways that I have seen is to pass required args and then an array of args that the receiving function can use, like: $this->db->bind('ii', array('someoption'=>$id1, 'feature'=>$id2, 'action'=>'doit')); Need more info on the desired outcome. -Shawn

« previous php.general (#277780) next »