PHP 调用具有可变数量参数的函数
所以我遇到了一些问题。我知道一种解决方案,但它看起来不太干净,我想知道是否有更好的解决方案。
我正在编写一个 MySQLi 包装器来运行准备好的语句。由于它是一个包装器并且旨在重用(动态),因此返回的列数取决于查询并且不是静态的。
我发现的一个解决方案是 call_user_func_array
,而且似乎每个人都使用这个解决方案。
它有效,但我的问题是它让我创建了一个额外的引用数组,而我一开始就不需要这些引用。
例如:
<?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);
?>
现在我正在创建另一个数组,只是为了让 call_user_func_array 通过引用传递参数,因为它不遵守方法定义。
有没有办法让 call_user_func_array 遵守方法/函数定义?是否有更简洁的方法来调用具有可变数量参数的方法/函数?或者,是否有一个整体更清洁、更好的解决方案来解决这个问题?
我在调用 bind_param
时也遇到了类似的问题,同时尝试保持能够更改绑定参数并完整地重新运行语句的功能。
在某种程度上,下面的代码到底在做什么。
$row[$field->name] = &$row[$field->name];
它奇怪地工作并且不会产生任何错误,尽管我不完全了解变量如何引用自身。除了我没有创建另一个数组之外,它和前者之间还有什么重大区别吗?是不是更好了?
谢谢。
编辑:
这就是我使用 Reflection API 而不是 call_user_fun_array
的方式。任何有关我的使用情况、性能与 call_user_func_array 、实现等方面的意见将不胜感激。我以前没有真正搞过它,几乎每种方法都没有记录,所以我不太确定使用它们的正确方法。
<?php
foreach ($fields as $field) {
$row[$field->name] = NULL;
}
$refMethod = new ReflectionMethod($stmt, 'bind_result');
$refMethod->invokeArgs($stmt, $row);
?>
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);
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道的一种方法是您可以使用 Reflection 类的
isPassedByReference
方法。这是我在面对这个问题后提出的解决方案我一直在开发的一个框架。
One way i know of is that you can use the Reflection class's
isPassedByReference
method.Here is a solution i came up with after facing that issue for a framework i have had been developing.