简体   繁体   English

PHP将数组传递给函数

[英]PHP passing array to function

I've seen a few people ask a similar question. 我见过一些人问过类似的问题。 But I do need a little more clarification on this particular subject. 但我确实需要对这一特定主题进行更多澄清。

I have several functions that pass several arguments. 我有几个函数传递几个参数。 I even have a few that are about 10 arguments. 我甚至有一些约10个论点。 (didn't initially plan it, simply grew over time) (最初没有计划,只是随着时间的推移而增长)

I don't have any problems looking at my source code to find out what the 7th argument is, but it does get tedious. 查看我的源代码以查找第7个参数是什么没有任何问题,但它确实变得乏味。 Most of the time I know what arguments to pass, just not the position. 大多数时候我都知道要传递什么参数,而不是位置。

I had a few ideas to simplify the process for me. 我有一些想法来简化我的流程。

a) pass 1 argument, but divide everything with some delimiter. a)传递1个参数,但用一些分隔符划分所有内容。 (but that's a bad idea!, since I still need to remember the position of each. (但这是一个坏主意!因为我仍然需要记住每个人的位置。

function myfunc('data|data|data|'){
          // explode the string
}

b) pass an array with key and values, and look for the key names inside my function, and act accordingly. b)传递一个带有键和值的数组,并在我的函数中查找键名,并采取相应的行动。

function myfunc(array('arg1' => 'blah blah', 'arg2' => 'more blah')){
  // loop through the array
}

c) keep it as it is. c)保持原样。

function myfunc($arg1,$arg2,$arg3,$arg4,$arg5........){
// yay

}

So, I'm seeking other options and better ideas for handling functions with growing argument lists. 所以,我正在寻找其他选项和更好的想法来处理函数增长参数列表。

In my opinion, the best way to it is by passing in an associative array. 在我看来,最好的方法是传入一个关联数组。 That way you immediately see what each argument does. 这样你就可以立即看到每个参数的作用。 Just be sure to name them descriptively, not arg1 & arg2 . 请务必以描述性方式命名,而不是arg1arg2

Another advantage an associative array, is that you don't have to care about the order in which the arguments are being passed in. 关联数组的另一个优点是,您不必关心传递参数的顺序。

Just remember that if you use an associative array, you lose PHP's native way of assigning default values, eg: 请记住,如果使用关联数组,则会丢失PHP本机分配默认值的方法,例如:

function doSomething($arg1 = TRUE, $arg2 = 55) { }

So, what you have to do is create your own set of default options, and then merge your arrays: 因此,您需要做的是创建自己的一组默认选项,然后合并您的数组:

function doSomething( $props = array() )
{
    $props = array_merge(array(
        'arg1' => TRUE,
        'arg2' => 55
    ), $props);

    // Now use the $props array
}

go with the myfunc($options) form when you have many optional parameters that can be mixed and matched. 如果有许多可混合和匹配的可选参数,请使用myfunc($options)表单。 Use array_merge_recursive with a default options array, and you'll be golden. 使用array_merge_recursive和一个默认的选项数组,你将是金色的。

b)(关联数组)是迄今为止最容易,最适应的(因为它允许模拟默认关键字参数),并且最不容易出错的变体。

First, see if you can introduce paramter object . 首先,看看你是否可以引入参数对象 It not necesseary to replace all 7 parameters, but maybe you can lower their count. 替换所有7个参数不是必要的,但也许你可以降低它们的数量。

Next, examine the function itself - why it needs so many parameters, maybe it just does to much things? 接下来,检查函数本身 - 为什么它需要这么多参数,也许它只是做了很多事情? Is Replace parameter with method / Replace parameter with explicit method applicable? 使用显式方法的 方法 / 替换参数 替换参数是否适用?

By the way, Refactoring is great reading! 顺便说一下,R​​efactoring很棒!

根据函数的用途,有时传递一个对象是合适的。

Let's not talk about solution #1. 我们不要谈论解决方案#1。 You already say that it is bad... 你已经说过它很糟糕......

I like the second idea of passing in this hash/associative array. 我喜欢传递这个散列/关联数组的第二个想法。 It feels rubylike :) The thing you are doing is that you are giving up some checks that you would get for free. 感觉像是rubylike :)你正在做的事情就是放弃一些你可以免费获得的支票。 If you call a function and forget an argument the runtime complains and the script stops executing. 如果你调用一个函数并忘记一个参数,那么运行时会抱怨并且脚本会停止执行。 In your scenario you would have to do all those checks inside your function: Are all necessary options given? 在您的场景中,您必须在函数内执行所有这些检查:是否提供了所有必要的选项? Are the values the right types, etc? 价值观是正确的类型等吗?

I would say, the decision is up to your taste if it's just you working on the code. 我想说,如果只是你在处理代码,那么决定取决于你的品味。 If others are working with you, I would ask them about their opinion. 如果其他人与你合作,我会询问他们的意见。 Until then, I would stick with the classic approach of having the function with 7 arguments 在那之前,我会坚持使用带有7个参数的函数的经典方法

Associative array with keys as parameter names, order does not count (do not to forget you document which keys are expected): 使用键作为参数名称的关联数组,顺序不计数(不要忘记你记录了预期的键):

function myFunc(array $args)
{
    # default values
    $args += array('arg1' => 'default1', 'arg2' => 'default2');
}

Sometimes you want to verify that you drop all unimportant keys from the input as well. 有时您想要验证是否也从输入中删除了所有不重要的键。 A useful function for this is array_intersect_key Docs . 一个有用的功能是array_intersect_key 文档

check this post about creating an ellipsis in PHP. 查看这篇关于在PHP中创建省略号的帖子。
Creating an ellipsis in PHP 在PHP中创建省略号

function myfunc($arg1,$arg2,$arg3,$arg4,$arg5........){ function myfunc($ arg1,$ arg2,$ arg3,$ arg4,$ arg5 ........){

With the limited information provided, I would have to say keep it as is . 由于提供的信息有限,我不得不说保持原样 The reason for this is that your function signature shows that none of your arguments have default values (they are all required). 原因是您的函数签名显示您的参数都没有默认值(它们都是必需的)。

If you make it an associative array, then you remove the parser's ability to warn about ill-called functions, ie: 如果你使它成为一个关联数组,那么你删除了解析器警告不良函数的能力,即:

Missing argument 2 for some_function, called in [etc.]

IDEs can help with function calling assistance if you have difficulty remembering all the arguments. 如果您难以记住所有参数,IDE可以帮助提供函数调用。

Basically, if it's a required argument, it should be in the function signature. 基本上,如果它是必需的参数,它应该在函数签名中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM