使用关联数组作为PHP函数的输入 [英] Using an associative array as php function's input

查看:164
本文介绍了使用关联数组作为PHP函数的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

偶尔我会写一个单输入,包含所有的该函数的输入的关联数组PHP函数。这有优点,例如不必记住输入正确的命令,但我也注意到它使实施更改到大codebases容易得多;当我需要再添变数,而变数必须经过4或5 pre-现有的其他功能,它更容易的时候,我可以只是坚持它在数组中,并沿传递。

我的问题是,是否有一个缺点这样做?

我很少看到的例子这样写的功能,或在开源我用,这使我相信有可能是一个缺点。如果没有的<​​em>不是的理由不这样做,为什么不这样写的所有功能?

更新

感谢您的答案。它看起来像两个主要问题突出:

code可读性 - 不可能知道哪些变量正在进入一个功能,他们是什么

可变蠕变 - 可能风与大规模阵列反弹从一个功能到下一个;一个人不应该传递参数不需要它们的功能

这都是伟大的点我也没多想。

似乎一般要旨以及是code其中这是一个问题,可能应被转换为一类。遗憾的是在这个特定项目这样的重构超出了范围,但我也认为比尔Karwin的解决方案是好的 - 传递可选的数组变量


解决方案

  

为什么不这样写的所有功能?


对于这个问题,为什么不忘记参数完全和使用全局变量的一切? (开玩笑)

传递一个关联数组有一个非常有用的优势:你可以让多个函数的参数可选的,你可以通过为* N *个参数的值,而不必传递一个值的* N-1 *个参数<。 / p>

但是你没有办法让一个编译时错误强制参数,如果你不通过他们。无论是你可以声明类型检查。

您必须写被调用的函数code来检查presence和所需参数的类型。

我用另一种方法是对于那些强制申报的常规参数,然后作为最后(可选)的说法,声明一个名为 $选项的关联数组只包含可选项目。

 函数database_connect($ DBNAME,$用户,$密码,数组$选项=阵列())

Occasionally I'll write a PHP function with a single input, an associative array containing all of that function's inputs. This has benefits such as not having to remember the correct order of inputs, but I've also noticed it makes implementing changes to large codebases much easier; when I need to add another variable, and that variable has to pass through 4 or 5 pre-existing functions, it's much easier when I can just stick it in the array and pass it along.

My question is, is there a downside to doing this?

I rarely see functions written this way in examples, or in open source I use, which leads me to believe there is probably a downside. If there isn't a reason not to do it then why not write all functions this way?

UPDATE

Thanks for all your answers. It looks like two major problems stand out:

Code readability - impossible to tell what variables are going into a function and what they're for

Variable creep - Could wind up with massive arrays bouncing from one function to the next; one should not pass parameters to functions that don't require them

Which are both great points I did not think about.

It seems the general gist as well is that code where this is an issue, should probably be converted to a class. Unfortunately in this specific project such a refactoring is beyond the scope, but I also think Bill Karwin's solution is good - pass an array of optional variables

解决方案

why not write all functions this way?

For that matter, why not forget about parameters completely, and use global variables for everything? (kidding)

Passing an associative array has one useful advantage: you can make multiple function parameters optional, and you can pass a value for the *N*th parameter without having to pass a value for the *N-1*th parameter.

But you have no way to make mandatory parameters with a compile-time error if you don't pass them. Neither can you declare type-checking.

You'll have to write code inside the called function to check for the presence and the type of required parameters.

An alternative I have used is to declare conventional parameters for those that are mandatory, and then as the last (optional) argument, declare an associative array called $options that contains only the optional items.

function database_connect($dbname, $user, $password, array $options = array())

这篇关于使用关联数组作为PHP函数的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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