問題描述
可能的重復(fù):
是否可以傳遞參數(shù)通過引用使用 call_user_func_array()?
我有以下代碼行在 PHP 5.1 中有效,但在 PHP 5.3 中無效.
I have the following line of code which worked in PHP 5.1, but isn't working in PHP 5.3.
$input = array('ss','john','programmer');
call_user_func_array(array($mysqli_stmt, 'bind_param'), $input);
在 PHP 5.3 中,我收到以下警告消息:
In PHP 5.3, I get the following warning message:
警告:mysqli_stmt::bind_param() 的參數(shù) 2 應(yīng)為參考,值在/var/www/startmission/em/class/cls.data_access_object.php 第 785 行
Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given in /var/www/startmission/em/class/cls.data_access_object.php on line 785
我將代碼更改為以下內(nèi)容并且它起作用了:
I changed the code to the following and it worked:
$a = 'johnl';
$b = 'programmer';
$mysqli_stmt->bind_param('ss',$a,$b);
我在 php 文檔中找到了這個:
I found this in the php documentation:
在使用 mysqli_stmt_bind_param() 時必須小心與 call_user_func_array() 結(jié)合使用.注意mysqli_stmt_bind_param() 需要通過引用傳遞參數(shù),而 call_user_func_array() 可以接受一個列表作為參數(shù)可以表示引用或值的變量.
Care must be taken when using mysqli_stmt_bind_param() in conjunction with call_user_func_array(). Note that mysqli_stmt_bind_param() requires parameters to be passed by reference, whereas call_user_func_array() can accept as a parameter a list of variables that can represent references or values.
所以我的問題是,如何復(fù)制 call_user_func_array + bind_params 的功能,以便我可以在運(yùn)行時動態(tài)綁定變量?
So my question is, how do I replicate the functionality of the call_user_func_array + bind_params such that i can dynamically bind variables at run time?
推薦答案
我在 fabio at Kidopi dot com dot br3 年前在 mysqli_stmt::bind_param() 的 PHP 手冊頁
(略有修改):
I found the answer to my problem in a user note by fabio at kidopi dot com dot br3 years ago on the PHP manual page of mysqli_stmt::bind_param()
(slightly modified):
在遷移到 php 5.3 后,我曾經(jīng)在使用 call_user_func_array
和 bind_param
時遇到問題.
I used to have problems with
call_user_func_array
andbind_param
after migrating to php 5.3.
原因是 5.3 需要數(shù)組值作為引用,而 5.2 使用真實(shí)值(但也使用引用).所以我創(chuàng)建了一個輔助函數(shù)來幫助我解決這個問題:
The cause is that 5.3 requires array values as reference while 5.2 worked with real values (but also with references). So I created a secondary helper function to help me with this:
function refValues($arr)
{
$refs = array();
foreach ($arr as $key => $value)
{
$refs[$key] = &$arr[$key];
}
return $refs;
}
并將我以前的功能從:
call_user_func_array(array($this->stmt, "bind_param"), $this->values);
到:
call_user_func_array(array($this->stmt, "bind_param"), refValues($this->values));
這樣我的 db 函數(shù)就可以在 PHP 5.2/5.3 服務(wù)器上繼續(xù)工作.
This way my db functions keep working in PHP 5.2/5.3 servers.
這篇關(guān)于php5.3 - mysqli_stmt:bind_params 帶有 call_user_func_array 警告的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!