back to article Named arguments squeak into PHP 8.0, 7 years after first RFC

The next major version of the PHP language will support named arguments after 76 per cent of lead developers voted to include it. PHP 8.0 is now expected to be released in November 2020 and feature freeze is on 4 August. The RFC (Request For Comments) for Named Arguments was submitted in September 2013 but revived for the PHP …

  1. Anonymous Coward
    Anonymous Coward

    "because you call it like, true, true, false, like what does this mean?"

    The presence of the extraneous "like" indicates the sentence was written by a millennial; this automatically and irrefutably means PHP is unreliable shit and should not be touched by anyone who wants to get a real job done, not involving beards and beanbags.

    1. Joe W Silver badge

      Re: "because you call it like, true, true, false, like what does this mean?"

      Read the text. It is the transcript of a podcast.

      I'm sure you always talk in a way that is already print-ready. The rest of us are mere mortals.

      1. logicalextreme

        Re: "because you call it like, true, true, false, like what does this mean?"

        I'm pretty sure that was a joke.

    2. wolfetone Silver badge

      Re: "because you call it like, true, true, false, like what does this mean?"

      OK Boomer.

    3. Anonymous Coward
      Anonymous Coward

      Re: "because you call it like, true, true, false, like what does this mean?"

      Like, the film Valley Girl came out in 1983? And it, like, totally starred Deborah Foreman, born 1962, and Nicholas Cage, born 1964? For sure.

      And of course "like" as interjection, clausal adverb, particle, or copula was well-known before the 1980s. It was part of the Beatnik dialect, as exemplified by Maynard G. Krebs in the Dobie Gillis television series, which premiered in 1959; and by the time something like that hit television in the '50s, it had to be well-established. I don't remember offhand whether it appears in the dialog in the original Gidget novel (1957), which was painstakingly researched, but I wouldn't be surprised.

      In other words, "like" as interjection was widely used by Boomers.

    4. Anonymous Coward
      Anonymous Coward

      Re: "because you call it like, true, true, false, like what does this mean?"

      Well, we now know that 16 millennials read The Reg on their parent-purchased iPads in-between oiling their beards and drinking single-estate avocado flavoured coffee at their jobs in 'digital social cloud UX'.

  2. karlkarl Silver badge

    Passing (true, true, false) is probably still shorter (and more explicit) than named arguments.

    Unlike C++ default arguments, I can't even quickly see what the default values are either for those that I don't explicitly name.

    My only experience with named arguments is Blender's bpy Python API and I must admit it was more effort than it was worth.

    So sure, they can add this to the language but I would rather developers didn't require it in public APIs. Its annoying.

  3. Anonymous Coward
    Anonymous Coward

    You can add in the names as comments to make the code readable:

    FunctionName( /* name1 */ true, /* name2 */ true, /* name3 */ false);

    Or for that matter, define three boolean local variables with sensible names and assign them before the call.

    1. TRT Silver badge

      I mean... a nice idea though JetBrains PHP tools already cross reference the PHPdocs and tooltip the parameter list. It could come into its own though when adding extra parameters to a function - if you have a long list of defaults which you barely ever use, then add a really useful one which most of your function calls need to pass...

    2. bpfh

      Use phpdoc or javadoc comments

      @argument type description

    3. logicalextreme

      True, but "you can already do it in this cumbersome way" shouldn't necessarily be allowed to shoot down a new language feature — even PHP should be allowed to evolve.

    4. Anonymous Coward Silver badge
      Boffin

      Or pass the argument list as an associative array (aka map)

      add_to_basket(array( 'customerID' => $custid, 'productID' => $prodid, 'quantity' => $qty ));

      Simple & already works, just need to write the function definitions correctly and provide suitable defaults.

  4. Chris Gray 1

    Possibilities...

    Another way for boolean parameters is to define an enumeration type for each parameter. Lots of typing once, but nice for the calls. If you define the enumeration tags nicely, then it is all perfectly clear. I didn't do that in any of my languages, however, because of scope issues with the tags, etc.

    Having parameters with default values is likely OK, so long as you don't allow the function to change the defaults. If the defaults are always, e.g., 0, 0.0, false, "", then I expect readability is OK. As a bonus, you can add parameters and most calls don't require changes.

  5. thames

    The main use for keyword (named) arguments in Python is when you have a function with a number of arguments, some of which you would most commonly want to leave in their default state, but which you sometimes need to change. You can use non-keyword arguments for the normal mandatory parameters, and keyword arguments for the optional parameters.

    For example:

    x = example('a')

    x = example('a', language='fr')

    In the above example, if you don't specify the "language" parameter it will remain at whatever the default is. You can however override that default by specifying a value. It's convenient in that you can offer options without having to call them if you are satisfied with the defaults.

    The other common use is for adding new arguments to a function that already takes a variable number of arguments. By adding the new argument as a keyword (named) one you don't break backward compatibility with existing code.

    Suppose we have a function which can take one or two parameters:

    x = example('a')

    x = example('a', 'b')

    Now we want to add another parameter:

    x = example('a')

    x = example('a', 'b')

    x = example('a', language='fr')

    x = example('a', 'b', language='fr')

    The keyword argument doesn't get confused with the existing optional positional argument.

    Keyword (named) arguments probably aren't something that you want to use everywhere. They are meant to solve specific problems where you either want to offer seldom used options without requiring them to be specified all the time. Also, they allow options to be added to existing functions instead of creating multiple versions of similar functions (e.g. examplefunc, examplefunc2, examplefunc3, etc.) with different parameter options.

    Like a lot of features it can be tiresome if overused, but it has its place when used judiciously.

    The above describes their use in Python. From what I can see in the PHP proposal, the intent is the same there.

  6. jmccabe

    Bizarre, and sad, that you still have people appearing to claim that named arguments are A Bad Thing! Software engineering isn't about how quickly you can type in your code; it's about long-term understanding, reliability and maintainability, and named arguments help achieve that. As far as the list of languages PHP joins is concerned, named arguments have been in use in Ada since before any of those languages were even a twinkle in someone's eye!

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon