News for hackers. Stuff that matters.
Re:Named arguments are coming in PHP 8 by arcosdev on Monday July 27, 2020 @02:07AM (#23962594)
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 by raziel2p on Monday July 27, 2020 @03:35AM (#23962955)
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.

Re:Named arguments are coming in PHP 8 by wonderlg on Monday July 27, 2020 @09:05AM (#23964671)
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
    })
Re:Named arguments are coming in PHP 8 by duckerude on Monday July 27, 2020 @11:43AM (#23966169)
PHP's strict typing is only enforced at runtime, and because of that, its type system is very simple, unlike Typescript's. You can enforce that a variable is an array, but not that it's an array of ints, let alone that it's an array with certain types under certain keys.

Although there is external tooling that uses static analysis for such more complicated types.

Re:Named arguments are coming in PHP 8 by xellisx on Tuesday July 28, 2020 @09:53PM (#23982740)
We really need structs in PHP. I drew up a draft, but didn't get very far with it. https://github.com/ellisgl/PHP-RFC-Struct-Data-Type
Re:Named arguments are coming in PHP 8 by webignition on Monday July 27, 2020 @02:19AM (#23962642)
I'd think that such a solution doesn't solve but merely moves the problem.

A method with N arguments, where N > 2, with a call that is messy due to the number of arguments could instead take a single argument being a parameter object where the constructor for the parameter object takes the same N arguments.

A constructor with N arguments with a call that is messy due to the number of arguments ...

Re:Named arguments are coming in PHP 8 by SAI_Peregrinus on Monday July 27, 2020 @09:38AM (#23964942)
Aah, so then you use the Builder Pattern! Instead of one constructor taking N arguments, have a constructor that takes no arguments and N function calls to initialize the constructed values. It's cleaner because it's a named pattern! /s
Re:Named arguments are coming in PHP 8 by flemhans on Monday July 27, 2020 @02:12AM (#23962611)
That's what the above comment was referring to when saying people to things like:

  call_user_func_array($function, ['length' => $n, 'foo' => $bar, ...]);
Re:Named arguments are coming in PHP 8 by stareatgoats on Monday July 27, 2020 @02:48AM (#23962752)
> and basically nobody does that anyway.

That's it, I'm officially a nobody :-)

Re:Named arguments are coming in PHP 8 by sildur on Monday July 27, 2020 @02:59AM (#23962805)
> 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.

You don’t check the input types?

Re:Named arguments are coming in PHP 8 by stareatgoats on Monday July 27, 2020 @04:46AM (#23963303)
You're reading the sentence incorrectly: "and basically nobody does that anyway", means the preceding type checking argument you refer to is moot. So I insist, I am a nobody (according to this definition).

Btw I don't always sanity check the parameters, mostly at the system edges only.

Re:Named arguments are coming in PHP 8 by z92 on Monday July 27, 2020 @05:23AM (#23963483)
Not in get-it-done, hackish scripts.
Re:Named arguments are coming in PHP 8 by Lammy on Monday July 27, 2020 @03:48AM (#23963016)
You can get the best of both approaches in PHP 5.6 or later using the argument unpacking "..." operator that works similarly to the */"splat" operator in Python or Ruby: https://www.php.net/manual/en/migration56.new-features.php
Re:Named arguments are coming in PHP 8 by gremlinsinc on Monday July 27, 2020 @09:47AM (#23965027)
it's not the best of both worlds though, splat should only be used on something where you're basically doing the same thing to a bunch of things.

like add(1,2,3,4,5) ->

  function add(...$nums){
    $count = 0;
      foreach($nums as $num) { $count += $num; }
  }
If you know what arguments you're getting, then the more sound way would be just use an associative array, but either way you really don't get great type checking.

This is a much better solution.

Re:Named arguments are coming in PHP 8 by gremlinsinc on Monday July 27, 2020 @09:42AM (#23964985)
I do this all the time, but it feels wrong... and you don't get type checking, this feels like it makes more sense and logical.
Re:Named arguments are coming in PHP 8 by beberlei on Monday July 27, 2020 @03:16AM (#23962877)
That is possible, but requires writing a class, which increases mental load
Re:Named arguments are coming in PHP 8 by gvjddbnvdrbv on Monday July 27, 2020 @03:36AM (#23962960)
I've tried it but it gets messy.