将parse_str用于可选函数参数与数组相比是否有优势?

时间:2022-02-05 11:12:48

I happened to be making some changes to a WordPress blog and noticed that they use parse_str (http://php.net/parse_str) for parsing and setting their optional parameters to a function.

我碰巧对WordPress博客进行了一些更改,并注意到他们使用parse_str(http://php.net/parse_str)解析并将其可选参数设置为函数。

I'm wondering if there is an advantage to this over sending an array?

我想知道发送阵列是否有优势?

Examples:

With array:

$blahOptions = array(
    'option_1' => true,
);
BlahArray($blahOptions);
function BlahArray($options = array()) {
    $defaults = array(
        'option_1' => false,
        'option_2' => 'blah',
    );
    // this would probably be in a function to be used everywhere
    foreach ($defaults as $defaultOption => $defaultValue) {
        if (!isset($options[$defaultOption])) $options[$defaultOption] = $defaultValue;
    }
}

With parse_str:

$blahOptions = 'option_1=1';
BlahString($blahOptions);
function BlahString($options = '') {
    $defaults = array(
        'option_1' => false,
        'option_2' => 'blah',
    );
    parse_str($options, $defaults);
    $options = $defaults;
}

3 个解决方案

#1


No. That seems like a ridiculous way to pass functional parameter arguments. I could understand it if you needed to recreate $_GET or $_POST variables or something along those lines, but for parameters to a function? That's code smell right there.

不。这似乎是一种传递功能参数参数的荒谬方式。如果你需要重新创建$ _GET或$ _POST变量或类似的东西,但是对于函数的参数,我能理解它吗?那是代码味道。

They should be using an array, and then utilizing extract() instead. I've worked with Wordpress before, and my advice is to keep the code at arm's length. It is not an example of model programming.

他们应该使用数组,然后使用extract()代替。我之前使用过Wordpress,我的建议是让代码保持一定的距离。它不是模型编程的一个例子。

#2


No. There are more disadvantages than advantages.

不是。缺点多于优点。

When you’re using a single string, you just can pass string values. With an array you can use every PHP data type and every element’s value type is independently of each other.

当您使用单个字符串时,您只需传递字符串值即可。使用数组,您可以使用每种PHP数据类型,并且每个元素的值类型彼此独立。

#3


With parse_str, you can potentially drop in the query string from the URL, and the function will work. If you use an array, and you want to use the query string, you'll have to enumerate everything into an array before calling the function.

使用parse_str,您可以从URL中删除查询字符串,该函数将起作用。如果使用数组,并且想要使用查询字符串,则必须在调用函数之前将所有内容枚举到数组中。

I'm not totally convinced it's the best way to go, but I see how it can add a bit of flexibility.

我并不完全相信这是最好的方式,但我知道它是如何增加一点灵活性的。

#1


No. That seems like a ridiculous way to pass functional parameter arguments. I could understand it if you needed to recreate $_GET or $_POST variables or something along those lines, but for parameters to a function? That's code smell right there.

不。这似乎是一种传递功能参数参数的荒谬方式。如果你需要重新创建$ _GET或$ _POST变量或类似的东西,但是对于函数的参数,我能理解它吗?那是代码味道。

They should be using an array, and then utilizing extract() instead. I've worked with Wordpress before, and my advice is to keep the code at arm's length. It is not an example of model programming.

他们应该使用数组,然后使用extract()代替。我之前使用过Wordpress,我的建议是让代码保持一定的距离。它不是模型编程的一个例子。

#2


No. There are more disadvantages than advantages.

不是。缺点多于优点。

When you’re using a single string, you just can pass string values. With an array you can use every PHP data type and every element’s value type is independently of each other.

当您使用单个字符串时,您只需传递字符串值即可。使用数组,您可以使用每种PHP数据类型,并且每个元素的值类型彼此独立。

#3


With parse_str, you can potentially drop in the query string from the URL, and the function will work. If you use an array, and you want to use the query string, you'll have to enumerate everything into an array before calling the function.

使用parse_str,您可以从URL中删除查询字符串,该函数将起作用。如果使用数组,并且想要使用查询字符串,则必须在调用函数之前将所有内容枚举到数组中。

I'm not totally convinced it's the best way to go, but I see how it can add a bit of flexibility.

我并不完全相信这是最好的方式,但我知道它是如何增加一点灵活性的。