PHP mysqli包装器:通过引用传递__call()和call_user_func_array() [英] PHP mysqli wrapper: passing by reference with __call() and call_user_func_array()
问题描述
我一直是stackoverflow的狂热粉丝,这是第一次海报.我很想看看是否有人可以帮助我.让我用一些代码深入研究,然后我将解释我的问题.我有以下包装器类:
I'm a long running fan of stackoverflow, first time poster. I'd love to see if someone can help me with this. Let me dig in with a little code, then I'll explain my problem. I have the following wrapper classes:
class mysqli_wrapper
{
private static $mysqli_obj;
function __construct() // Recycles the mysqli object
{
if (!isset(self::$mysqli_obj))
{
self::$mysqli_obj = new mysqli(MYSQL_SERVER, MYSQL_USER, MYSQL_PASS, MYSQL_DBNAME);
}
}
function __call($method, $args)
{
return call_user_func_array(array(self::$mysqli_obj, $method), $args);
}
function __get($para)
{
return self::$mysqli_obj->$para;
}
function prepare($query) // Overloaded, returns statement wrapper
{
return new mysqli_stmt_wrapper(self::$mysqli_obj, $query);
}
}
class mysqli_stmt_wrapper
{
private $stmt_obj;
function __construct($link, $query)
{
$this->stmt_obj = mysqli_prepare($link, $query);
}
function __call($method, $args)
{
return call_user_func_array(array($this->stmt_obj, $method), $args);
}
function __get($para)
{
return $this->stmt_obj->$para;
}
// Other methods will be added here
}
我的问题是,当我在mysqli_stmt_wrapper
类上调用bind_result()
时,我的变量似乎没有通过引用传递,并且什么也没有返回.为了说明这一点,如果我运行这段代码,我只会得到NULL:
My problem is that when I call bind_result()
on the mysqli_stmt_wrapper
class, my variables don't seem to be passed by reference and nothing gets returned. To illustrate, if I run this section of code, I only get NULL's:
$mysqli = new mysqli_wrapper;
$stmt = $mysqli->prepare("SELECT cfg_key, cfg_value FROM config");
$stmt->execute();
$stmt->bind_result($cfg_key, $cfg_value);
while ($stmt->fetch())
{
var_dump($cfg_key);
var_dump($cfg_value);
}
$stmt->close();
我还从PHP中得到一个很好的错误,告诉我:PHP Warning: Parameter 1 to mysqli_stmt::bind_result() expected to be a reference, value given in test.php on line 48
I also get a nice error from PHP which tells me: PHP Warning: Parameter 1 to mysqli_stmt::bind_result() expected to be a reference, value given in test.php on line 48
我试图重载bind_param()
函数,但是我不知道如何通过引用获取可变数量的参数. func_get_args()
似乎也无法提供帮助.
I've tried to overload the bind_param()
function, but I can't figure out how to get a variable number of arguments by reference. func_get_args()
doesn't seem to be able to help either.
如果像$stmt->bind_result(&$cfg_key, &$cfg_value)
那样通过引用传递变量,它应该可以工作,但这是不建议使用的行为,并且会引发更多错误.
If I pass the variables by reference as in $stmt->bind_result(&$cfg_key, &$cfg_value)
it should work, but this is deprecated behaviour and throws more errors.
有人对此有想法吗?非常感谢您的宝贵时间.
Does anyone have some ideas around this? Thanks so much for your time.
推荐答案
如果从mysqli_stmt类扩展,将绕过参考问题. (没有干净的解决方案)
If you'll extend from the mysqli_stmt class you'll bypass the reference problem. (which has no clean solution)
class mysqli_stmt_wrapper extends mysqli_stmt {
public function __construct($link, $query) {
parent::__construct($link, $query);
}
}
class mysqli_wrapper extends mysqli {
public function prepare($query) {
return new mysqli_stmt_wrapper($this, $query);
}
}
这篇关于PHP mysqli包装器:通过引用传递__call()和call_user_func_array()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!