So I've run into a bit of an issue. I know of a solution but it doesn't seem very clean and I'm wondering if there's a better one.
I'm writing a MySQLi wrapper for running prepared statements. As it's a wrapper and is meant to be reused (dynamic) the amount of columns returned depends on the query and isn't static.
The one solution to this that I've found, and seems is what everyone uses, is call_user_func_array
.
It works, but my issue with it is it has me creating an extra array of references that I shouldn't really need in the first place.
For example:
<?php
// Connect, prepare statement, etc.
$stmt->execute();
$fields = $stmt->result_metadata();
$fields = $fields->fetch_fields();
foreach ($fields as $field) {
// Unnecessary creation of an array.
$params[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $params);
?>
So now I'm creating another array just to get call_user_func_array
to pass the parameters by reference because it doesn't adhere to the method definition.
Is there some way to get call_user_func_array
to adhere to method / function definitions? Is there a cleaner way of calling a method / function with a variable number of parameters? Or, is there just an overall cleaner and better solution to this issue all together?
I've also run into similar problems when calling bind_param
while trying to keep the feature of being able to change the bound parameters and rerunning the statement intact.
On somewhat of a side issue, what exactly is the code below doing.
$row[$field->name] = &$row[$field->name];
It oddly works and doesn't produce any errors, though I don't exactly get how a variable can reference itself. Are there any major differences between that and the former other than the fact I'm not creating another array? Is it better?
Thanks.
EDIT:
This is how I used the Reflection API instead of call_user_fun_array
. Any input on my usage of it, performance vs. call_user_func_array
, implementation, etc. would be greatly appreciated. I haven't really messed with it before and just about every method is undocumented so I'm not really sure of the proper way to use them.
<?php
foreach ($fields as $field) {
$row[$field->name] = NULL;
}
$refMethod = new ReflectionMethod($stmt, 'bind_result');
$refMethod->invokeArgs($stmt, $row);
?>