Pass by reference problem with PHP 5.3.1

2019-01-08 21:44发布

Ok, this is a weird problem, so please bear with me as I explain.

We upgraded our dev servers from PHP 5.2.5 to 5.3.1.

Loading up our code after the switch, we start getting errors like:

Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in /home/spot/trunk/system/core/Database.class.php on line 105

the line mentioned (105) is as follows:

call_user_func_array(Array($stmt, 'bind_param'), $passArray);

we changed the line to the following:

call_user_func_array(Array($stmt, 'bind_param'), &$passArray);

at this point (because allow_call_time_pass_reference) is turned off, php throws this:

Deprecated: Call-time pass-by-reference has been deprecated in /home/spot/trunk/system/core/Database.class.php on line 105

After trying to fix this for some time, I broke down and set allow_call_time_pass_reference to on.

That got rid of the Deprecated warning, but now the Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference warning is throwing every time, with or without the referencing.

I have zero clue how to fix this. If the target method was my own, I would just reference the incoming vars in the func declaration, but it's a (relatively) native method (mysqli).

Has anyone experienced this? How can I get around it?

Thank you.

9条回答
Explosion°爆炸
2楼-- · 2019-01-08 22:12

I've got a similar problem, the current code didnt work:

$query="Select id,name FROM mytable LIMIT ?,?";
$params=Array('ii');
array_push($params,$from_var);
array_push($params,$to_var);
...
$stmt=$link->prepare("$query");
$ref=new ReflectionClass('mysqli_stmt');
$method=$ref->getMethod("bind_param");
$method->invokeArgs($stmt,$params);
...

It told that "Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given"

And then, in despair, I've tried to take $from_var and $to_var in quotes. And it worked!

$params=Array('ii');
array_push($params,"$from_var");
array_push($params,"$to_var");

Hope, it will help somebody, good luck :)

查看更多
孤傲高冷的网名
3楼-- · 2019-01-08 22:13

I think what is deprecated is passing a reference through a function. In the function definition you do something like:

function(&$arg) {

}

This doesn't help you much but you probably need not pass the reference anyway. I guess you could try a wrapper function.

function wrapper($stmt, &$passArray) {
    call_user_func_array($stmt, $passArray);
}
查看更多
放荡不羁爱自由
4楼-- · 2019-01-08 22:21

You are passing an array of elements ($passArray). The second item inside the passed array needs to be a reference, since that is really the list of items you are passing to the function.

查看更多
登录 后发表回答