Live data from Hacker News

Named arguments are coming in PHP 8

stitcher.io

91–100 of 140 posts

Re: Named arguments are coming in PHP 8

#91
It's pretty funny that named arguments were, for a long time, a feature present only in Objective-C of all the languages I tried (and I tried many).

It was one of the features of Objective-C I liked and I missed in any other language. I also think they were one of the reasons many developers disliked the language.

When Swift was introduced, it got named parameters from Objective-C and now they are getting more "mainstream", entering in a language much more widespread than the former two.

Re: Named arguments are coming in PHP 8

#94

It's pretty funny that named arguments were, for a long time, a feature present only in Objective-C of all the languages I tried (and I tried many). It was one of the features of Objective-C I liked and I missed in any other language. I also think they were one of the reasons many developers disliked the language. When Swift was introduced, it got named parameters from Objective-C and now they are getting more "mains…

Python has had named arguments for a long time (since v1.4), which is arguably more widespread than PHP or Swift.

Re: Named arguments are coming in PHP 8

#95
post #89

Earlier quoted context omitted.

> if you over supply a function by position today PHP doesn't error I'd argue that this it the worrying part. How often do you intentionally supply an extra argument, and how often is it an accident? I'd take the warning.

It's important to recognise what we can't do with that error in: 25, 'name' => 'Brent', 'email' => 'brent@stitcher.io', ]; function send_email(string $name, string $email) : void { //... } send_email(...$user); ^So this would error, even though it's a nice specification as far as the function is concerned, I'm all for strict static analysis but not at the expense of open maps "If your program deals with information,…

While you're right, I don't think that's something commonly used. I definitely can't remember the last time I wrote code where this would be useful.

That might be because usage of associative arrays is more or less discouraged in modern PHP in favor of value objects (especially with property promotion in PHP8). Main reason for that is that this usage is horrible for IDEs and static analysis. Way too easy to rename a parameter and create a runtime error. Typing is also enforced at usage instead of creation, and you have to repeat the type information in every function signature.

Re: Named arguments are coming in PHP 8

#96

I've often wondered why there was no "default" keyword, so you could call a function like this: foo('first arg', default, 'third arg'); Named parameters will do but they are more verbose. Speaking whishes, method overloading would be great.

Having switched from 80% Java (20% about 5 other languages) to probably 75% Kotlin over the last year, one of my absolute favorite things named arguments.

When you name your arguments, simply supplying them will use the default if one has been specified. If one hasn’t been specified then you must provide it when calling and your compiler/IDE will definitely let you know.

I used to go out of my way with method overloads, JavaDocs, argument @annotations, and Builder classes in order to get this same level conciseness and cleanliness in my Java code. It is extra work (even with Lombok) but I personally can’t stand when I don’t keep it clean like that. Understandably a lot of other developers on my teams don’t always go to the same lengths.

I honestly haven’t ready the details on PHP’s plan for this, but with Kotlin’s names arguments I’ve found almost no need of method overloading anymore- most of those input variations are now covered (except for when you’ve got different types coming in, which usually can be handled with a common interface or base class).

Re: Named arguments are coming in PHP 8

#99
post #9

Earlier quoted context omitted.

Why not use a dictionary or parameter object when you get past 2 arguments? Or is that not a thing in PHP?

With a dictionary (array in PHP) you lose strict typing and it's possible for the person constructing it to make a typo and it won't cause an error, the called function will just think an optional argument was left out. Writing and maintaining parameter classes is just tedious.

Typescript can do it, so why not add the same feature to PHP as well?

As a JS/TS developer, all I see is avoiding two characters: {}, everything else is possible in JavaScript and well-typed in TypeScript.

PHP 8

    save(
      value: 3
    )
JS 1

    save({
      value: 3
    })
Post reply on HN