Live data from Hacker News

Named arguments are coming in PHP 8

stitcher.io

1–10 of 140 posts

Re: Named arguments are coming in PHP 8

#4
post #3

in python i started writing any function that takes >2 arguments as either def foo(*,a,b,c)... or def foo(obvious_and_required_thing, *, a,b,c) ... and noticed an immediate improvement in clarity.

What does * represent here? The "self" object?

It‘s a separator between positional and keywords. It’s * because the “rest” argument (*args) also makes following arguments keyword-only, so a lone star is essentially one which collects nothing.

Re: Named arguments are coming in PHP 8

#5

in python i started writing any function that takes >2 arguments as either def foo(*,a,b,c)... or def foo(obvious_and_required_thing, *, a,b,c) ... and noticed an immediate improvement in clarity.

Thank you very much. I am using python for some time now. Stumbled upon this, but never took the time to read up on it. If anybody also wants to know more I found this extensive post explaining the concepts behind * and in python (not in multiplication): https://treyhunner.com/2018/10/asterisks-in-python-what-they...

Re: Named arguments are coming in PHP 8

#6
post #4
post #3

Earlier quoted context omitted.

What does * represent here? The "self" object?

It‘s a separator between positional and keywords. It’s * because the “rest” argument (*args) also makes following arguments keyword-only, so a lone star is essentially one which collects nothing.

So this requires the following args to be named? Neat!

Re: Named arguments are coming in PHP 8

#7
I'm ambivalent.

There's a tension in PHP-land between PHP's roots as a low-ish level, get-it-done, hackish language, with its big standard library and simple scalar types, and the better-organized and quite vocal developers who want it to be more Java-like, with great big frameworks and many deeply-nested complex class hierarchies. Instead of unwieldy hobbyist-hacker balls of mud, you build enterprise-scale balls of mud.

Named arguments don't do a lot for the first group. It looks like the big point in favor is not having to look up a reference for which-arguments-go-where in functions anymore, but a good IDE already does that for you.

But there is a common anti-pattern where you have some polymorphic function or interface, and you want to accrete your arguments somewhere and then bundle them up and call the function, so then you see things like [1]:

    call_user_func($function, ['length' => $n, 'foo' => $bar, ...]);
...and that is bad because it totally bypasses all of PHP's type-checking and leaves it up to the called function to sanity-check the input types instead of letting the compiler do it, and basically nobody does that anyway.

So named parameters would fix that at least, which would be nice. I'm just not looking forward to the impact this is going to have on code readability, because everyone (including this article) is going to go, "oh, now my parameter list is too long!", and break the function signature into one-line-per-parameter, which is vile.

[1]: I'm guilty of this too.

Re: Named arguments are coming in PHP 8

#8

I'm ambivalent. There's a tension in PHP-land between PHP's roots as a low-ish level, get-it-done, hackish language, with its big standard library and simple scalar types, and the better-organized and quite vocal developers who want it to be more Java-like, with great big frameworks and many deeply-nested complex class hierarchies. Instead of unwieldy hobbyist-hacker balls of mud, you build enterprise-scale balls of…

> break the function signature into one-line-per-parameter, which is vile

Speak for yourself. If each line indicates what it is the parameter for I couldn’t care less.

Nobody will use this for functions that have just two parameters. It’s the ones that have 10 possibilities that are crazy.

Re: Named arguments are coming in PHP 8

#9
post #8

I'm ambivalent. There's a tension in PHP-land between PHP's roots as a low-ish level, get-it-done, hackish language, with its big standard library and simple scalar types, and the better-organized and quite vocal developers who want it to be more Java-like, with great big frameworks and many deeply-nested complex class hierarchies. Instead of unwieldy hobbyist-hacker balls of mud, you build enterprise-scale balls of…

> break the function signature into one-line-per-parameter, which is vile Speak for yourself. If each line indicates what it is the parameter for I couldn’t care less. Nobody will use this for functions that have just two parameters. It’s the ones that have 10 possibilities that are crazy.

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

Re: Named arguments are coming in PHP 8

#10
post #9
post #8

Earlier quoted context omitted.

> break the function signature into one-line-per-parameter, which is vile Speak for yourself. If each line indicates what it is the parameter for I couldn’t care less. Nobody will use this for functions that have just two parameters. It’s the ones that have 10 possibilities that are crazy.

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

That's what the above comment was referring to when saying people to things like:

  call_user_func_array($function, ['length' => $n, 'foo' => $bar, ...]);
Post reply on HN