向数组添加空值
我有这个方法:
public function search($searchKey=null, $summary=null, $title=null, $authors=null, $paginationPage=0) {
...
}
我试图用这个检索所有参数:
$Class = new Search();
// Get parameters
$ReflectionMethod = new \ReflectionMethod($Class, "search");
try {
foreach($ReflectionMethod->getParameters() AS $Parameter) {
if(array_key_exists($Parameter->name, $this->params)) {
$parameters[$Parameter->name] = $this->params[$Parameter->name];
} elseif($Parameter->isDefaultValueAvailable()) {
$paramaters[$Parameter->name] = $Parameter->getDefaultValue();
} else {
...
}
} catch(\Exception $e) {
...
}
// Call function
return call_user_func_array(array($Class, "search"), $parameters);
我的 $this->params
具有以下内容:
array
'paginationPage' => int 2
'id' => int 30
'searchKey' => string 'test' (length=4)
因为 $summary、$title 和 $authors 不存在,所以它们将获得其默认值 null
。当为参数分配空值时,它将被跳过,这会导致 $parameters 数组看起来像这样:
array
'searchKey' => string 'test' (length=4)
'paginationPage' => int 2
这会导致方法调用如下:
public function search('test', 2, null, null, 0) {
...
}
虽然它应该是:
public function search('test', null, null, null, 2) {
...
}
希望您看到问题。如何确保这些空值也放入我的 $parameters
数组中。添加无效值是不可能的,因为它是用户输入,所以基本上可以是所有内容。
编辑
在上面的示例中,search
方法是硬编码的。但简化的事情之一是 search
实际上是一个变量,因此 search
可以是任何东西。这意味着我不知道该方法的参数是什么,并且无法在 foreach 循环之前预定义它们。预定义参数的解决方案实际上正是这段代码应该做的事情。
I have the this method:
public function search($searchKey=null, $summary=null, $title=null, $authors=null, $paginationPage=0) {
...
}
And I'm trying to retrieve all parameters with this:
$Class = new Search();
// Get parameters
$ReflectionMethod = new \ReflectionMethod($Class, "search");
try {
foreach($ReflectionMethod->getParameters() AS $Parameter) {
if(array_key_exists($Parameter->name, $this->params)) {
$parameters[$Parameter->name] = $this->params[$Parameter->name];
} elseif($Parameter->isDefaultValueAvailable()) {
$paramaters[$Parameter->name] = $Parameter->getDefaultValue();
} else {
...
}
} catch(\Exception $e) {
...
}
// Call function
return call_user_func_array(array($Class, "search"), $parameters);
My $this->params
has this content:
array
'paginationPage' => int 2
'id' => int 30
'searchKey' => string 'test' (length=4)
Because $summary, $title and $authors are not present, they will get their default value which is null
. When assigning a null value to an argument, it will be skipped which results in a $parameters array that looks like this:
array
'searchKey' => string 'test' (length=4)
'paginationPage' => int 2
Which result in a method call like:
public function search('test', 2, null, null, 0) {
...
}
While it should be:
public function search('test', null, null, null, 2) {
...
}
Hopefully you see the problem. How can I make sure those null values are also put into my $parameters
array. Adding an invalid value is not possible, because it is user input, so that can be basically everything.
Edit
In the example above the method search
is hardcoded. But one of the simplified things is that search
is actually a variable and because of that search
can be anything. This means that I don't know what the parameters of the method are and I can't predefine them before the foreach loop. The solution of predefining the parameters is actually exactly what this code should be doing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在进入
foreach
循环之前预初始化$parameters
怎么样:How about pre-initializing
$parameters
before entering theforeach
loop:天哪...这只是一个简单的拼写错误:
我真丢脸!
Oh my... It was just a simple typo:
Shame on me!