向数组添加空值

发布于 2024-10-05 04:52:48 字数 1762 浏览 4 评论 0原文

我有这个方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

电影里的梦 2024-10-12 04:52:48

在进入 foreach 循环之前预初始化 $parameters 怎么样:

$parameters = array(
    $searchKey => null,
    $summary => null,
    $title => null,
    $authors => null,
    $paginationPage => 0
);

How about pre-initializing $parameters before entering the foreach loop:

$parameters = array(
    $searchKey => null,
    $summary => null,
    $title => null,
    $authors => null,
    $paginationPage => 0
);
七婞 2024-10-12 04:52:48

天哪...这只是一个简单的拼写错误:

...
} elseif($Parameter->isDefaultValueAvailable()) {
    $paramaters[$Parameter->name] = $Parameter->getDefaultValue();
} else {
...

我真丢脸!

Oh my... It was just a simple typo:

...
} elseif($Parameter->isDefaultValueAvailable()) {
    $paramaters[$Parameter->name] = $Parameter->getDefaultValue();
} else {
...

Shame on me!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文