开发者

PHP's db2_bind_param()

Why do you have to specify variable names as a string val开发者_StackOverflowue with this function? Why can't I just use the variable name directly? This seems very silly to me.

Example: db2_bind_param($prepared, 1, "foo1");


As stated in the manual, it is the variable, to which the resource is bound to:

bool db2_bind_param(resource $stmt , int $parameter-number , string $variable-name, ...)

That way, you have a clean signature, because it always returns a boolean and no other data type. Other functions return either false or a resource. I believe, that the library has been ported from a strongly typed language like c or c++. In these languages, you can only specify one return type in the signature - something like bool|resource db2_bind_param(...) does not work.

In the end you are right: In a loosely typed language like PHP, you do not need it.


Possibly it's doing something like:

function db2_bind_param($stmt, $position, $name) {
   global $$name;
   $$name = &$stmt[$position];
}

just a guess, though...


That way you can bind a set of variables programmatically, as

$db_vars = array("foo1","foo2");

for($i=1; $i<=sizeof($db_vars); ++$i)
  db2_bind_param($prepared, $i, $db_vars[$i]);

You can then get the values programmatically using variable variables:

foreach($db_vars as $var)
  echo $var,'=',$$var,"\n";

If the $variable-name argument were expected to be the variable itself, a basic feature of the function abstraction would break, since you could pass in exactly the same value and get different results:

$foo = 'hello';
db2_bind_param2($prepared, 1, $foo); // feasible, but...

db2_bind_param2($prepared, 1, 'hello'); // should give the same result as above, but what would it be?
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜