7

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.

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.

1
  • 1
    +1 to @sanmai; also note that Perl, Python, and Ruby all support named parameters in the way you describe. If this feature is important enough, you should consider using one of those languages. Commented Oct 27, 2010 at 2:42

4 Answers 4

12

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

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

This seems like the kind of thing I should use. Will tick it as the correct answer when I am able to. Thanks.
Just thought of one thing - currently there are 4 arguments, two of which are optional. I want to add another 4. By implementing this method I would be reducing it to one argument (an array). This would break any calls to the function outside of the file I want to alter this in. Its been ages since I last did any PHP, but I am assuming that I could just create another function called the same thing but with a different number of arguments. Not ideal - but that should work.
@ClarkeyBoy You can have as many arguments as you wish. See additional example.
3

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

1 Comment

Thanks for the input - but I think that sanmai's answer suits my situation a bit better. With this method I would not be able to retrieve an argument by name - so I would hit the same brick wall (I think).
2

Named arguments are not currently available in 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.

Your original example would look something like:

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

Comments

0

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

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

 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.

2 Comments

What an overkill! Consider passing an object this way.
@sanmai: Always depends on the usage. Microoptimizations are for fools. And imaginary overhead is preferable to hindering yourself with a clumsy API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.