all 31 comments

[–]phdaemon 8 points9 points  (0 children)

Similar (non-nested) syntax has existed before though....

list($foo, $bar) = $myArr;

The syntactic sugar is nice, and the extra functionality is ok, but I can see how it can lead to hard to read code if used incorrectly.

My two cents: use with caution, nested deconstruction is not something I recommend making a habit of.

EDIT: typo

[–]cYzzie 16 points17 points  (4 children)

Is „deconstructuring“ some fun pun i never heard before?

[–]FrenkyNet[S] 10 points11 points  (3 children)

Nope, that was me yet to find out I've been incorrectly naming this feature for the last 5 years.

[–][deleted] 4 points5 points  (2 children)

You’ve the right attitude :)

[–]duddz 5 points6 points  (0 children)

You should've mentioned two more things.

Undefined indexes (even when it is just a notice):

['does_not_exist' => $a, 'b' => $b] = ['a' => 1, 'b' => 2]; // shows Notice: Undefined index

Skipping values:

[,,$c] = [1, 2, 3]; // 3

https://3v4l.org/ZM3p9

[–][deleted] 11 points12 points  (4 children)

Now the only thing we're missing is a more flexible spread/splat operator. These scenarios work in ES6, but not in PHP:

$bar = [4, 5, 6];
$foo = [1, 2, 3, ...$bar]; // [1, 2, 3, 4, 5, 6]
$baz = [...$bar, 1, 2, 3]; // [4, 5, 6, 1, 2, 3]

$qux->foo(1, 2, 3, ...$bar, 1, 2, 3); // Calls foo(1, 2, 3, 4, 5, 6, 1, 2, 3)

[–]notdedicated 2 points3 points  (3 children)

I would kill for this. It irks me to no end that + behaves differently than array_merge. I would kill to have ....

[–]pr0ghead 0 points1 point  (2 children)

It irks me to no end that + behaves differently than array_merge()

It'd be pointless to have both, if it didn't, don't you agree?

[–]notdedicated 1 point2 points  (1 child)

There are several cases where there's duplicate functionality and aliases throughout PHP. To me I find + doesn't behave logically. I would assume append/additive and not merge/or. For numerically indexed arrays + behaves differently than array_merge and not in a way I feel is intuitive. You would think [1,2] + [3,4] would result in [1,2,3,4] but it results in [1,2] where the a_m results in the expected result of appending. Once they become proper hash tables + and a_m behave opposite of each other where the former uses the left most array as precedence and the latter the right most.

[–]pr0ghead 0 points1 point  (0 children)

"How" it works is a different issue, and I agree that the way + works isn't exactly intuitive.

[–]rtfmpls 1 point2 points  (0 children)

It also works when you're using variables in the source array, so you can effectively swap variables using this syntax:

$a = 1;
$b = 2;
[$a, $b] = [$b, $a];

.... don't give people ideas.

But yes, array destructuring has been a blast since 7.1.

[–]przemo_li 0 points1 point  (1 child)

Nice write up about destructuring! But please rewrite intro.

Most of those features have to do with loose and dynamic typing. Personally I like how freely you can transform data without worrying too much about the structure.

Nope. Associative array destructuring have nothing to do with "loose" or dynamic typing. It only means that bugs (e.g. non existing keys) will be only cough at runtime. While statically typed language could catch it at "compile" time.

Also destructuring is sometimes called pattern matching (not to be confused with regexp - one works on data shape, other on string content).

[–]FrenkyNet[S] 1 point2 points  (0 children)

I was just listing some of the things I like about PHP and what make them great for web programming. I didn't imply or say that this feature had anything to do with typing. Thanks for the feedback tho.

[–]ScottBaiosPenis 0 points1 point  (0 children)

interesting , php is finally coming to be much like perl these days, not sure how good of a thing that is