views:

1401

answers:

7

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.

A: 

I think the mysqli_bind_param() and mysqli_bind_result() functions are very awkward to use. I've encountered the same difficulty as you describe using them in combination with call_user_func_array()

My workaround was to stop using mysqli and instead use PDO_mysql. It has a much easier usage:

$pdoStmt->execute( $passArray );
Bill Karwin
I thought PDO_mysql was deprecated?
Spot
No, not at all. What in the world gave you that idea?
Bill Karwin
@Spot: `php_mysql.so` is not recommended (not deprecated), `pdo_mysql.so` is very much recommended.
Andrew Moore
+3  A: 

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.

Zak
Actually you are right. However referencing that var does not help either. Hmmmm
Spot
Ok, fixed this by creating a dummy array, referencing the values from the main. It's quite sad that this was required to be honest. :)Thanks!
Spot
A: 

The second paramer Must be an array. apparently this was only enforced in 5.3

DC

DeveloperChris
It <b>is</b> an array.
Spot
what's in that array?
DeveloperChris
A: 

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);
}
Louis
+1  A: 

I just experienced this same problem, calling bind_param via call_user_func_array and passing an array of parameters. The solution is to modify the values in the array to be referenced. It's not elegant but it works.

call_user_func_array(array($stmt, 'bind_param'), makeValuesReferenced($passArray));

function makeValuesReferenced($arr){
    $refs = array();
    foreach($arr as $key => $value)
        $refs[$key] = &$arr[$key];
    return $refs;

}
Dominic
+1  A: 

Actually, be aware that there is a bug with PHP 5.3.1 concerning references and all call family of functions:

PHP Bugs #50394: Reference argument converted to value in __call

The behavior you are seeing might be a result of this bug and any attempt to fix it code wise may cause problems in the long run.

The problem has been fixed in the SVN version of PHP. Until 5.3.2 is released, you may compile a new version for use, or downgrade to an earlier version.

Andrew Moore
+1  A: 

We were experiencing this same problem with this code:

call_user_func(array($strCartHandler, 'CartPurchaseEvent'), $strCartEvent, $objToUser, null, $this);

My solution was to just skip call_user_func altogether and do this:

$strCartHandler::CartPurchaseEvent($strCartEvent, $objToUser, null, $this);
leek