我的 PHP 函数应该接受参数数组还是应该明确请求参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2112913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:04:35  来源:igfitidea点击:

Should my PHP functions accept an array of arguments or should I explicitly request arguments?

phpoverloading

提问by John

In a PHP web application I'm working on, I see functions defined in two possible ways.

在我正在处理的 PHP Web 应用程序中,我看到以两种可能的方式定义的函数。

Approach 1:

方法一:

function myfunc($arg1, $arg2, $arg3)

Approach 2:

方法二:

// where $array_params has the structure array('arg1'=>$val1, 'arg2'=>$val2, 'arg3'=>$val3)
function myfunc($array_params)

When should I use one approach over another? It seems that if system requirements keep changing, and therefore the number of arguments for myfunc keep changing, approach 1 may require a lot of maintenance.

我什么时候应该使用一种方法而不是另一种方法?似乎如果系统需求不断变化,因此 myfunc 的参数数量不断变化,方法 1 可能需要大量维护。

采纳答案by John Parker

If the system is changing so often that using an indexed array is the best solution, I'd say this is the least of your worries. :-)

如果系统变化如此频繁以至于使用索引数组是最好的解决方案,我会说这是您最不担心的。:-)

In general functions/methods shouldn't take too many arguments (5 plus or minus 2 being the maximum) and I'd say that you should stick to using named (and ideally type hinted) arguments. (An indexed array of arguments only really makes sense if there's a large quantity of optional data - a good example being configuration information.)

通常,函数/方法不应采用太多参数(最大为 5 个正负 2 个),我会说您应该坚持使用命名(理想情况下键入提示)参数。(只有在有大量可选数据的情况下,参数的索引数组才真正有意义——配置信息就是一个很好的例子。)

As @Pekka says, passing an array of arguments is also liable to be a pain to document and therefore for other people/yourself in 'n' months to maintain.

正如@Pekka 所说,传递一系列参数也很容易记录下来,因此其他人/你自己在“n”个月内维护。

Update-ette...

更新...

Incidentally, the oft mentioned book Code Completeexamines such issues in quite a bit of detail - it's a great tome which I'd highly recommend.

顺便提一下,经常提到的《代码完成》一书非常详细地研究了这些问题——这是一本我强烈推荐的好书。

回答by Pekka

Using a params array (a surrogate for what is called "named arguments" in other languages") is great - I like to use it myself - but it has a pretty big downside: Arguments are not documentable using standard phpDoc notation that way, and consequently, your IDE won't be able to give you hints when you type in the name of a function or method.

使用 params 数组(其他语言中所谓的“命名参数”的替代品)很棒——我喜欢自己使用它——但它有一个很大的缺点:使用标准的 phpDoc 表示法无法以这种方式记录参数,并且因此,当您输入函数或方法的名称时,您的 IDE 将无法给您提示。

回答by Bryan M.

I find using an optionalarray of arguments to be useful when I want to override a set of defaults in the function. It might be useful when constructing an object that has a lot of different configuration options or is just a dumb container for information. This is something I picked up mostly from the Ruby world.

当我想覆盖函数中的一组默认值时,我发现使用可选的参数数组很有用。当构造一个具有许多不同配置选项的对象或者只是一个愚蠢的信息容器时,它可能很有用。这是我主要从 Ruby 世界中学到的东西。

An example might be if I want to configure a container for a video in my web page:

例如,如果我想在我的网页中为视频配置一个容器:

function buildVideoPlayer($file, $options = array())
{
  $defaults = array(
    'showAds' => true,
    'allowFullScreen' = true,
    'showPlaybar' = true
  );

 $config = array_merge($defaults, $options);

 if ($config['showAds']) { .. }
}

$this->buildVideoPlayer($url, array('showAds' => false));

Notice that the initial value of $options is an empty array, so providing it at all is optional.

注意 $options 的初始值是一个空数组,所以提供它是可选的。

Also, with this method we know that $options will always be an array, and we know those keys have defaults so we don't constantly need to check is_array()or isset()when referencing the argument.

此外,使用这种方法,我们知道 $options 将始终是一个数组,并且我们知道这些键具有默认值,因此我们不需要经常检查is_array()isset()在引用参数时。

回答by Bryan M.

with the first approach you are forcing the users of your function to provide all the parameters needed. the second way you cannot be sure that you got all you need. I would prefer the first approach.

使用第一种方法,您将迫使函数的用户提供所需的所有参数。第二种方式你不能确定你得到了你需要的一切。我更喜欢第一种方法。

回答by Bryan M.

If the parameters you're passing in can be grouped together logically you could think about using a parameter object (Refactoring, Martin Fowler, p295), that way if you need to add more parameters you can just add more fields to your parameter class and it won't break existing methods.

如果您传入的参数可以在逻辑上组合在一起,您可以考虑使用参数对象(Refactoring,Martin Fowler,p295),这样如果您需要添加更多参数,您只需向参数类添加更多字段即可它不会破坏现有的方法。

回答by GSto

There are pros and cons to each way.

每种方式都有利有弊。

  • If it's a simple function that is unlikely to change, and only has a few arguments, then I would state them explicitly.

  • If the function has a large number of arguments, or is likely to change a lot in the future, then I would pass an array of arguments with keys, and use that. This also becomes helpful if you have function where you only need to pass some of the arguments often.

  • 如果它是一个不太可能改变的简单函数,并且只有几个参数,那么我会明确说明它们。

  • 如果函数有大量参数,或者将来可能会发生很大变化,那么我会传递一个带键的参数数组,然后使用它。如果您有只需要经常传递一些参数的函数,这也会很有帮助。

An example of when I would choose an array over arguments, for an example, would be if I had a function that created a form field. possible arguments I may want:

例如,当我选择一个数组而不是参数时的一个例子是,如果我有一个创建表单字段的函数。我可能想要的可能参数:

  • Type
  • Value
  • class
  • ID
  • style
  • options
  • is_required
  • 类型
  • 价值
  • 班级
  • ID
  • 风格
  • 选项
  • 是必须的

and I may only need to pass a few of these. for example, if a field is type = text, I don't need options. I may not always need a class or a default value. This way It is easier to pass in several combinations of arguments, without having a function signature with a ton arguments and passing null all the time. Also, when HTML 5 becomes standard many many years from now, I may want to add additional possible arguments, such as turning auto-complete on or off.

我可能只需要通过其中的几个。例如,如果一个字段是 type = text,我就不需要选项。我可能并不总是需要一个类或一个默认值。这种方式可以更容易地传入多个参数组合,而无需使用带有 ton 参数的函数签名并始终传递 null。此外,当许多年后 HTML 5 成为标准时,我可能想添加其他可能的参数,例如打开或关闭自动完成功能。

回答by Aris

I know this is an old post, but I want to share my approach. If the variables are logically connected in a type, you can group them in a class, and pass an object of them to the function. for example:

我知道这是一个旧帖子,但我想分享我的方法。如果变量在一个类型中逻辑连接,您可以将它们分组在一个类中,并将它们的一个对象传递给函数。例如:

      class user{
           public $id;
           public $name;
           public $address;
           ...
      }

and call:

并调用:

      $user = new user();
      $user->id = ...
      ...

      callFunctions($user);

If a new parameter is needed you can just add it in the class and the the function signature doesn't need to change.

如果需要一个新参数,您可以将它添加到类中,函数签名不需要更改。