PHP功能设计:传递参数 [英] PHP function design: passing parameters

查看:84
本文介绍了PHP功能设计:传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想说我不想在这里发动战争。我知道可以从我自己的对话中获得一些关于以下主题的强烈意见,所以我正在寻找一个概述利弊的答案。



在松散类型的语言(我特别来自PHP的立场),从设计角度来说,有两种方法可以传递参数(注意,我不是说在被调用函数中获取参数)。



一方面,您可以将它们作为单个参数传递,如下所示:

foo($ var1,$ var2,$ var3 ...);另一方面,你可以将你的变量打包到一个数组中然后传递一个参数给函数,数组:

  $ bar [] = $ var1; 
$ bar [] = $ var2;
$ bar [] = $ var3;
...

foo($ bar);

当您需要处理大量变量时,我可以看到第二种方法的好处然而,它以一种通用的方式隐藏了函数为了正确操作而需要的变量。

第一种解决方案虽然很快成为一个长函数调用,变量数量增加,更好地显示了函数需要的变量,并且如果函数调用中传递了空值,则可以利用功能来分配默认值。

因此,在什么情况下应该使用一个或另一个,以及中间的情况如何,例如,数组是关联的,以便您可以通过名称在函数中获取变量并以非通用方式使用它们?对我来说,关联数组的场景看起来非常不理想,而且只是一些开发人员使用的方式,使得调用本身的函数本身更短,同时实际增加了写入的代码总量。 解决方案

我认为你传递数组的例子是毫无意义的。如果你这样做,你应该使用正常的方式 传递参数。

传递一个数组只有在函数接受多个可选参数它可以以几乎任何顺序传递:

 函数x($ width = 100,$ height = 100,$ color = 'red')

这里,如果您想更改颜色,但不更改默认宽度你需要做的高度:

  x(100,100,'blue'); 

I。你可能需要在你的代码中复制默认值。



相反,可以声明如下函数:

<$ ('width'=> 100,'height'=> 100,'color'=> p $ p> function x(array $ opts){
$ opts + = array '红');
}
x(array('color'=>'blue'));

对于更实际的示例,请查看 Twig_Environment :当你可以看到,有相当多的选择,他们中的大多数可能会希望保持其默认值。如果您必须将所有这些选项作为参数传递,则只需复制默认值即可使代码膨胀。


Firstly, I want to say I'm not trying to start a war here. I'm aware there can be some strongly held opinions from my own conversations on the topic that follows so I am looking for an answer that outlines the pros and cons.

In loosely typed languages (and I'm specifically coming from a PHP standpoint), there are two ways, in design terms, to pass parameters (note, I'm not talking about getting the parameters within the called functions).

On the one hand you can pass them as individual parameters as below:

foo($var1, $var2, $var3 ...);

On the other hand you can package up your variables into an array and then pass a single argument to the function, the array:

 $bar[] = $var1;
 $bar[] = $var2;
 $bar[] = $var3;
 ...

 foo($bar);

I can see the benefit of the second method when you have a lot of variables that need to be handled in a generic way, however, it also hides what variables the function needs in order to operate correctly.

The first solution, while it quickly becomes a long function call as the number of variables increases, better shows what variables the function requires and enables leveraging functionality to assign a default value if a null value is passed in the function call.

So, under what circumstances should one or the other be used and what about the middle ground, where for example, the array is associative so you can get the variables out by name inside the function and use them in a non-generic way? To me the associative array scenario seems very un-optimal and is simply a way that some developers use to make the function call itself shorter while actually increasing the overall amount of code written.

解决方案

I think that your example of passing an array is pretty pointless. If you do this you should definitely pass the arguments using the normal way.

Passing an array is only useful if a function accepts several optional parameters which could be passed in pretty much any order:

function x($width = 100, $height = 100, $color = 'red')

Here, in case you want to change the color, but not change the default width and height you would need to do:

x(100, 100, 'blue');

I.e. you would need to copy the default values in your code.

Instead one could declare the function as follows:

function x(array $opts) {
    $opts += array('width' => 100, 'height' => 100, 'color' => 'red');
}
x(array('color' => 'blue'));

For a more real-life example, have a look at the constructor of Twig_Environment: As you can see, there are quite some options, most of them you will probably want to be kept at their default value. If you had to pass all of this options as an argument, you would just bloat the code by copying default values.

这篇关于PHP功能设计:传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆