Re: Re: Variable number of parameters

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Oops! Meant to send this to the list....

On Aug 7, 2008, at 9:31 AM, Philip Thompson wrote:

On Aug 6, 2008, at 4:25 PM, Shawn McKenzie wrote:

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

The bind_param() function is a built-in PHP MySQLi function (http://php.net/mysqli_bind_param ). I can't change how this accepts args (of course). Thanks for your thoughts anyway. =D

~Philip



"innerHTML is a string. The DOM is not a string, it's a hierarchal object structure. Shoving a string into an object is impure and similar to wrapping a spaghetti noodle around an orange and calling it lunch."


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux