PHP可选参数 - 按名称指定参数值?

时间:2021-01-13 10:57:04

I know it is possible to use optional arguments as follows:

我知道可以使用可选参数,如下所示:

function doSomething($do, $something = "something") {

}

doSomething("do");
doSomething("do", "nothing");

But suppose you have the following situation:

但是假设你有以下情况:

function doSomething($do, $something = "something", $or = "or", $nothing = "nothing") {

}

doSomething("do", $or=>"and", $nothing=>"something");

So in the above line it would default $something to "something", even though I am setting values for everything else. I know this is possible in .net - I use it all the time. But I need to do this in PHP if possible.

因此,在上面的行中,即使我为其他所有设置值,它也会默认$ something到某个东西。我知道这在.net中是可行的 - 我一直都在使用它。但是如果可能的话,我需要在PHP中执行此操作。

Can anyone tell me if this is possible? I am altering the Omnistar Affiliate program which I have integrated into Interspire Shopping Cart - so I want to keep a function working as normal for any places where I dont change the call to the function, but in one place (which I am extending) I want to specify additional parameters. I dont want to create another function unless I absolutely have to.

任何人都可以告诉我这是否可能?我正在改变我已经整合到Interspire购物车中的Omnistar联盟计划 - 所以我想保持一个功能正常工作,我不改变对该功能的调用的任何地方,但在一个地方(我正在扩展)我想要指定其他参数。除非我绝对必须,否则我不想创建另一个函数。

4 个解决方案

#1


11  

No, in PHP that is not possible as of writing. Use array arguments:

不,在PHP中,写作时是不可能的。使用数组参数:

function doSomething($arguments = array()) {
    // set defaults
    $arguments = array_merge(array(
        "argument" => "default value", 
    ), $arguments); 

    var_dump($arguments);
}

Example usage:

用法示例:

doSomething(); // with all defaults, or:
doSomething(array("argument" => "other value"));

When changing an existing method:

更改现有方法时:

//function doSomething($bar, $baz) {
function   doSomething($bar, $baz, $arguments = array()) {
    // $bar and $baz remain in place, old code works
}

#2


3  

Have a look at func_get_args: http://au2.php.net/manual/en/function.func-get-args.php

看一下func_get_args:http://au2.php.net/manual/en/function.func-get-args.php

#3


2  

Named arguments are not currently available in PHP (5.3).

PHP(5.3)中目前不提供命名参数。

To get around this, you commonly see a function receiving an argument array() and then using extract() to use the supplied arguments in local variables or array_merge() to default them.

为了解决这个问题,你通常会看到一个接收参数array()的函数,然后使用extract()在局部变量或array_merge()中使用提供的参数来默认它们。

Your original example would look something like:

您的原始示例如下所示:

$args = array('do' => 'do', 'or' => 'not', 'nothing' => 'something');
doSomething($args);

#4


0  

PHP has no named parameters. You'll have to decide on one workaround.

PHP没有命名参数。您必须决定一种解决方法。

Most commonly an array parameter is used. But another clever method is using URL parameters, if you only need literal values:

最常用的是数组参数。但另一个聪明的方法是使用URL参数,如果您只需要文字值:

 function with_options($any) {
      parse_str($any);    // or extract() for array params
 }

 with_options("param=123&and=and&or=or");

Combine this approach with default parameters as it suits your particular use case.

将此方法与默认参数相结合,因为它适合您的特定用例。

#1


11  

No, in PHP that is not possible as of writing. Use array arguments:

不,在PHP中,写作时是不可能的。使用数组参数:

function doSomething($arguments = array()) {
    // set defaults
    $arguments = array_merge(array(
        "argument" => "default value", 
    ), $arguments); 

    var_dump($arguments);
}

Example usage:

用法示例:

doSomething(); // with all defaults, or:
doSomething(array("argument" => "other value"));

When changing an existing method:

更改现有方法时:

//function doSomething($bar, $baz) {
function   doSomething($bar, $baz, $arguments = array()) {
    // $bar and $baz remain in place, old code works
}

#2


3  

Have a look at func_get_args: http://au2.php.net/manual/en/function.func-get-args.php

看一下func_get_args:http://au2.php.net/manual/en/function.func-get-args.php

#3


2  

Named arguments are not currently available in PHP (5.3).

PHP(5.3)中目前不提供命名参数。

To get around this, you commonly see a function receiving an argument array() and then using extract() to use the supplied arguments in local variables or array_merge() to default them.

为了解决这个问题,你通常会看到一个接收参数array()的函数,然后使用extract()在局部变量或array_merge()中使用提供的参数来默认它们。

Your original example would look something like:

您的原始示例如下所示:

$args = array('do' => 'do', 'or' => 'not', 'nothing' => 'something');
doSomething($args);

#4


0  

PHP has no named parameters. You'll have to decide on one workaround.

PHP没有命名参数。您必须决定一种解决方法。

Most commonly an array parameter is used. But another clever method is using URL parameters, if you only need literal values:

最常用的是数组参数。但另一个聪明的方法是使用URL参数,如果您只需要文字值:

 function with_options($any) {
      parse_str($any);    // or extract() for array params
 }

 with_options("param=123&and=and&or=or");

Combine this approach with default parameters as it suits your particular use case.

将此方法与默认参数相结合,因为它适合您的特定用例。