有什么方法可以在 PHP 中指定可选参数值吗?
假设我有一个 PHP 函数 foo:
function foo($firstName = 'john', $lastName = 'doe') {
echo $firstName . " " . $lastName;
}
// foo(); --> john doe
有没有办法只指定第二个可选参数?
例子:
foo($lastName='smith'); // output: john smith
Let's say I've got a PHP function foo:
function foo($firstName = 'john', $lastName = 'doe') {
echo $firstName . " " . $lastName;
}
// foo(); --> john doe
Is there any way to specify only the second optional parameter?
Example:
foo($lastName='smith'); // output: john smith
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
PHP 本身不支持函数的命名参数。 但是,有一些方法可以解决这个问题:
PHP does not support named parameters for functions per se. However, there are some ways to get around this:
数组技术的一种变体,可以更轻松地设置默认值:
用法:
A variation on the array technique that allows for easier setting of default values:
Usage:
您可以稍微重构您的代码:
You could refactor your code slightly:
如果您有多个可选参数,一种解决方案是传递一个哈希数组参数:
当然,在此解决方案中,不会验证哈希数组的字段是否存在或拼写是否正确。 这一切都由你来验证。
If you have multiple optional parameters, one solution is to pass a single parameter that is a hash-array:
Of course in this solution there's no validation that the fields of the hash array are present, or spelled correctly. It's all up to you to validate.
PHP 8 引入了命名参数,因此现在可以指定传递给函数的参数。
PHP 8 之前:
PHP 8 之后:
参考
PHP 8 introduced named arguments so now it's possible to specify the parameters passed to a function.
before PHP 8:
after PHP 8:
Reference
不。通常的方法是使用一些启发式方法来确定隐含的参数,例如字符串长度、类型等。
一般来说,您将编写函数以按照最需要到最少需要的顺序获取参数。
No. The usual way of doing this is with some heuristics to determine which parameter was implied, like string length, typing, etc.
Generally speaking, you'd write the function to take the parameters in the order of most required to least required.
你想要的方式:不。
您可以使用一些特殊标记,例如 NULL 来指出未提供值:
The way you want: no.
You could use some special mark, like NULL to note that value is not supplied:
我希望当我 2.5 年前开始使用 PHP 时,这个解决方案就已经存在了。 它在我创建的示例中效果很好,而且我不明白为什么它不应该完全可扩展。 与之前提出的方案相比,它具有以下优点:
(i)对函数内参数的所有访问都是通过命名变量进行的,就好像参数已完全声明一样,而不需要数组访问
(ii)它可以非常快速且轻松地适应现有的函数
(iii) 任何函数都只需要一行附加代码(除了不可避免地需要定义默认参数之外,您无论如何都会在函数签名中执行此操作,但您可以在数组中定义它们)。 额外线路的功劳完全归功于比尔·卡尔文。 该行对于每个函数都是相同的。
方法
使用强制参数和可选数组定义函数
将可选参数声明为局部变量
关键:使用通过数组传递的参数替换任何可选参数的预先声明的默认值。
调用该函数,传递其强制参数,仅传递您需要的可选参数
例如,
然后像这样调用它
结果:
狗站在驴上
猫坐在驴上
狗站在垫子上
I wish this solution had been on SO when I started using PHP 2.5 years ago. It works great in the examples I have created, and I don't see why it shouldn't be thoroughly extensible. It offers the following benefits over those proposed previously:
(i) all access to parameters within the function is by named variables, as if the parameters were fully declared, rather than requiring array access
(ii) it is very quick and easy to adapt existing functions
(iii) only a single line of additional code is required for any function (in addition to the inescapable necessity of defining your default parameters, which you would be doing in the function signature anyway, but instead you define them in an array). Credit for the additional line is wholly due to Bill Karwin. This line is identical for every function.
Method
Define your function with its mandatory parameters, and an optional array
Declare your optional parameters as local variables
The crux: replace the pre-declared default value of any optional parameters using those you have passed via the array.
Call the function, passing its mandatory parameters, and only those optional parameters that you require
For example,
and then call it like this
Results:
The dog stood on the donkey
The cat sat on the donkey
A dog stood on the mat
这里提到了一些“选项”风格的实现。 如果您打算将它们作为标准使用,那么到目前为止,没有一个是非常防弹的。 尝试这种模式:
这为您提供了函数局部变量(在本例中只是
$foo
),并防止创建任何没有默认值的变量。 这样一来,任何人都不会意外地覆盖函数内的其他参数或其他变量。There are a few 'options' style implementations mentioned here. None thus far are very bulletproof if you plan to use them as as standard. Try this pattern:
This gives you function-local variables (just
$foo
in this example) and prevents creating any variables that do not have a default. This is so no one can accidentally overwrite other parameters or other variables within the function.参数需要按位置顺序传递,您不能跳过参数本身; 您必须提供默认参数值才能跳过它。 可以说,这违背了您想要实现的目的。
无需重写函数以不同方式接受参数,这里有一种调用时方法来解决此问题:
换句话说,您使用反射来检查函数的参数并按名称将它们映射到可用参数,并跳过可选参数及其默认值价值。 是的,这很丑陋而且很麻烦。 您可以使用此示例创建一个如下函数:
Arguments need to be passed in order by position, you cannot skip a parameter per se; you'll have to supply the default parameter value in order to skip it. Arguably that defeats the purpose of what you're trying to achieve.
Without rewriting your function to accept parameters differently, here's a call-time way to work around this:
In other words, you're using reflection to inspect the function's parameters and map them to the available parameters by name, skipping optional parameters with their default value. Yes, this is ugly and cumbersome. You could use this sample to create a function like:
如果经常使用它,只需定义一个新的专用函数:
请注意,如果您愿意,您还可以在过程中更改 $lastname 的默认值。
当一个新函数被认为过于臃肿时,只需调用带有所有参数的函数即可。 如果你想让它更清楚,你可以将你的文字预先存储在 fin 命名变量中或使用注释。
好吧,这不像
person($lastName='Lennon')
那样简短和优雅,但似乎你不能在 PHP 中拥有它。 这并不是最性感的编码方式,使用超级元编程技巧或其他什么,但是您希望在维护过程中遇到什么解决方案?If this is used very often, just define a new specialized function :
Note that you could also change the default value of $lastname in the process if you wish.
When a new function is estimated overbloated, just call you function with all parameters. If you want to make it more clear, you can prestore your literals in fin named variable or use comments.
Ok, this is not as short and elegant as
person($lastName='Lennon')
, but it seems you can't have it in PHP. And that's not the sexiest way to code it, with super metaprogramming trick or whatever, but what solution would you prefer to encounter in a maintenance process?遗憾的是,您想要做的事情没有“语法糖”的方式来实现。 它们都是 WTF 的不同形式。
如果您需要一个带有未定义数量的任意参数的函数,
那么就可以了。 尽量避免过多使用它,因为对于 Php 来说这有点神奇。
然后,您可以选择应用默认值并根据参数计数执行奇怪的操作。
另外,您可以选择模拟 Perl 样式参数,
当然,在这里使用数组参数会更容易,但是您可以选择(即使是不好的方式)做事。
值得注意的是,这在 Perl 中更好,因为你可以只执行 foo(firstname => 'john' );
Sadly what you're trying to do has no "syntactic sugar" way of doing it. They're all varying forms of WTF.
If you need a function that takes an undefined number of arbitrary parameters,
Will do the trick. Try avoid using it too much, because for Php this is a bit on the magical side.
You could then optionally apply defaults and do strange things based on parameter count.
Also, you could optionally emulate Perl style parameters,
Of course, it would be easier to use an array argument here, but you're permitted to have choice ( even bad ways ) of doing things.
notedly, this is nicer in Perl because you can do just foo( firstname => 'john' );
不,没有,但你可以使用数组:
No there isn't but you could use an array: