News for hackers. Stuff that matters.
Re:Go with PHP by azangru on Thursday May 11, 2023 @03:45AM (#35899081)
> Read the following code as if it were a documentation page:

If I read this as a documentation page, this makes me scared:

`Order::create($validated + ['status' => 'pending']);`

What does it mean to add an array to something?

Re:Go with PHP by simonhamp on Thursday May 11, 2023 @04:14AM (#35899306)
This could be rewritten as:

Order::create([...$validated, 'status' => 'pending']);

Re:Go with PHP by azangru on Thursday May 11, 2023 @11:46AM (#35903868)
Ah, makes sense. Thank you.
Re:Go with PHP by BystanderX on Thursday May 11, 2023 @03:59AM (#35899166)
It would be shorthand for:

`$validated = ['key' => 'value']; $validated['status'] = 'pending'; Order::create($validated);`

Re:Go with PHP by Y-bar on Thursday May 11, 2023 @03:49AM (#35899100)
As someone who likes PHP that stumped me as well, I *really enjoy working with PHP 8.1, but I could not really grok the code either.
Re:Go with PHP by iLoveOncall on Thursday May 11, 2023 @03:50AM (#35899107)
Exactly the same as the spread operator in JavaScript or even the exact same syntax in Python, but we don't hear you complaining about those, do we?
Re:Go with PHP by maleldil on Thursday May 11, 2023 @07:35AM (#35900715)
> the exact same syntax in Python

No, because the example is showing concatenation of associative arrays. In Python, you'd use a dict, and you can't do `validated + {'status': 'pending'}`:

``` >>> validated = {} >>> validated + {'status': 'pending'} Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'dict' and 'dict' ```

You can do `validated | {'status': 'pending'}` since 3.9, though. I think using union or the * operator (e.g. `{*validated, 'status': 'pending'}`) looks less weird, but if you're a PHP programmer, you already know what + does here. In the end, it becomes bikeshedding.

Re:Go with PHP by azangru on Thursday May 11, 2023 @11:17AM (#35903402)
No complaints about the spread operator :-) But the plus in javascript produces this:

    [1,2,3] + [1,2,3]
    // '1,2,31,2,3'
(never tried this in js before; so was amused by the result)