Forum Moderators: coopster

Message Too Old, No Replies

Passing different numbers of arguments to functions

         

sned

4:40 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



Basically, I'm looking to turn this:

$array($val1, $val2, $val3, $val4, ...);

into this:

my_function($val1, $val2, $val3, $val4, ... );

There's all the func_num_args(), etc. but I'm not sure how to "explode" the array into the function list.

Has anyone had experience with something like this before?

Thanks!
-sned

whoisgregg

5:57 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can pass an array into a function. I often prefer to design my functions to accept a single array variable to make it more flexible in regards to variable order/presence.

<?php
function myfunction($arr){
if(is_array($arr)){
print_r( $arr );
} else {
echo 'Not an array!';
}
}
$array = array('red','blue','green');
myfunction($array);

sned

6:18 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



I actually use the array passing quite a bit, but unfortunately, it won't work in this case (I think).

I'm trying to abstract the mysqli_stmt_bind_param [us.php.net] function to something like this:

function db_bind_param($stmt, $array){
$types = '';
foreach($array as $value=>$type){
$types .= $type;
// do something with the value here to get $val1, $val2, $val3, etc.
}
return mysqli_stmt_bind_param($stmt, $types, $val1, $val2, $val3, ....);
}

Where $array is something like:


$array = array(
'this is my string'=>'s',
'12'=>'i'
);

phparion

6:37 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



so cant you pass array like the other fellow suggested? if can't then go for php searliaze and deserialize to pass array as string and then vice versa to work on it as array

whoisgregg

6:56 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahh... now I understand. :) Take a look at list [php.net]. I'm pretty sure it will do what you want.

coopster

9:53 pm on Apr 4, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Perhaps this thread will help too ...
The difference between various types of args [webmasterworld.com]

sned

10:16 pm on Apr 4, 2007 (gmt 0)

10+ Year Member



Ahh, thank you coopster. I think eval will do exactly what I'm looking for. Never thought of it for that context.

-sned

sned

6:08 pm on Apr 5, 2007 (gmt 0)

10+ Year Member



If you're curious, here's the final code from this particular function. It sort of grew to be a monstrosity, with variable variables. Useful? I thought it might be, but I'm not so sure now. I hope to combine the init, prepare, bind, and execute functions all into a single function called with a couple parameters.

At least it was a good learning experience with eval (and those variable variables always twist my mind around).


// bind parameters to a statement: params are sent as array($val=>'i', $val=>'d', etc...)
function db_stmt_bind_param($stmt, $params){
$types = '';
$values = array();
$x = 0;
foreach($params as $val=>$type){
$types .= $type;
$varname = 'var' . $x;
$$varname = $val;
$values[] = '$' . $varname;
$x++;
}
$temp = implode(',', $values);

$stmtvar = 'mystmt';
$$stmtvar = $stmt;

return eval("return mysqli_stmt_bind_param($$stmtvar, '$types', $temp);");
}

Thanks again for your help.
-sned

sned

11:05 pm on Apr 5, 2007 (gmt 0)

10+ Year Member



Boy -- I feel stupid. Found call_user_func_array [us.php.net]

Much cleaner:


function db_stmt_bind_param($stmt, $params){
$opts[] = $stmt;
foreach($params as $val=>$type){
$opts['type'] .= $type;
$opts[] = $val;
}
return call_user_func_array('mysqli_stmt_bind_param', $opts);
}