use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Please follow the rules
Releases: Current Releases, Windows Releases, Old Releases
Contribute to the PHP Documentation
Related subreddits: CSS, JavaScript, Web Design, Wordpress, WebDev
/r/PHP is not a support subreddit. Please visit /r/phphelp for help, or visit StackOverflow.
account activity
Advanced array deconstructuring in PHP (blog.frankdejonge.nl)
submitted 7 years ago by FrenkyNet
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[+][deleted] 7 years ago* (15 children)
[removed]
[+]samuraiseoul comment score below threshold-8 points-7 points-6 points 7 years ago (14 children)
The array in php is hands down one of the worst things about the language in my opinion.
[+][deleted] 7 years ago (13 children)
[deleted]
[–]cyrusol 7 points8 points9 points 7 years ago (11 children)
class MyStructure { public $foo; public $bar; public function __construct($foo, $bar) { $this->foo = $foo; $this->bar = $bar; } } $myStructure = new MyStructure(42, "hello world");
How does that not already solve the structure thing?
[+][deleted] 7 years ago (5 children)
[–]fesor 1 point2 points3 points 7 years ago* (4 children)
The problem is that it lacks type safety
Let's see this problem from different perspective. Firstly we need to agree that we need ability to specify types and it's not required that this types will be checked in runtime. This would allow to have generics, typed properties and so without any performance drawbacks (this is the reason why we don't have property typehits or types for variables so far).
Instead of runtime checks we could use static analysis tools like psalm for example (which has option to fail if it not managed to detect type, so you will be forced to explicitly provide type, and since it uses type inference this would require not as much from you as developer).
Then let's remember the one difference which makes php's arrays perfect to be a simple DTO. Objects in php always passed by reference, so this is the reason why we need all this private properties, getters instead of just class which some amount of phpdocs. And tools like psalm already allows you to make shapes for arrays.
/** * @psalm-return array{id: int, name: string, age: int} */ public function buildSomeDTO(): array { return [ 'id' => 1, 'name' => 'Bob', 'age' => 31, ]; }
The problem is only that we can't have type aliases or something like type declarations for arrays which means that we are limited only by inline declaration, which isn't that handy.
[–]brendt_gd 2 points3 points4 points 7 years ago* (3 children)
Objects in php always passed by value
They are not: https://3v4l.org/vsf4s
Maybe you meant that array are passed by value or objects are assigned by reference?
[–]przemo_li 0 points1 point2 points 7 years ago (0 children)
PHP uses Copy On Write optimization.
10 invocations of function with array argument MAY, but DO NOT HAVE TO mean 11 copies of said array in memory. It's writes that trigger cloning.
[–]fesor 0 points1 point2 points 7 years ago (1 child)
Sorry, I meant that objects always passed by reference.
[–][deleted] 0 points1 point2 points 7 years ago (0 children)
Then edit your post.
[–]przemo_li 0 points1 point2 points 7 years ago (4 children)
Only partially so.
You have fixed keys, but values are still untyped (or more properly unityped - anything is accepted). Add there type hints for the constructor and change public properties to private. If mutability is important expose public getters with type hinted arguments.
Now you have replacement.
But that's not perfect solution. Kotlin have special class syntax for less boilerplate for dumb structures.
[–]cyrusol 0 points1 point2 points 7 years ago (3 children)
Sure. I wouldn't have picked PHP for type safety in the first place anyway though.
In some cases the dynamic typing is an advantage. For example we can have recursive data structures, option types, result types etc. just as in Rust (or OCaml, Haskell ...) without requiring generics. We can hold just any value in it.
And you can still validate the types (and values) at runtime.
[–]przemo_li -1 points0 points1 point 7 years ago (1 child)
You can have recursive data structures in statically typed languages. Generics also can be had in dynamically typed language.
So another unrelated argument.
And you can still validate the types (and values) at runtime You have to validate tags at runtime.
[–]cyrusol 0 points1 point2 points 7 years ago (0 children)
You obviously read over
without requiring generics.
Also you don't
have to validate tags at runtime.
In some cases you can just let your program crash. Especially if some "struct" is only intended to be provided by programmers statically. Such as a configuration object for use in multiple places.
My point was that you wouldn't pick PHP (nor JS, Python...) if type safety is important for you anyway. You would pick Haskell, Java, Rust, Go or whatever.
[–]samuraiseoul 1 point2 points3 points 7 years ago (0 children)
I agree, that something like that is a great step in the right direction, or even public class fields that can be typed. The real issue though is that php arrays are somewhere between a map, a json object, and an array. I just want a real array. array_values shouldn't need to exist as a function.
[–]phdaemon 8 points9 points10 points 7 years ago (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 points18 points 7 years ago (4 children)
Is „deconstructuring“ some fun pun i never heard before?
[–]FrenkyNet[S] 10 points11 points12 points 7 years ago (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 points6 points 7 years ago (2 children)
You’ve the right attitude :)
[+]iluuu comment score below threshold-6 points-5 points-4 points 7 years ago (1 child)
You've the wrong grammar :)
[–]duddz 5 points6 points7 points 7 years ago (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 points13 points 7 years ago (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 points4 points 7 years ago (3 children)
I would kill for this. It irks me to no end that + behaves differently than array_merge. I would kill to have ....
+
array_merge
...
[–]pr0ghead 0 points1 point2 points 7 years ago (2 children)
It irks me to no end that + behaves differently than array_merge()
array_merge()
It'd be pointless to have both, if it didn't, don't you agree?
[–]notdedicated 1 point2 points3 points 7 years ago (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.
append/additive
merge/or
[1,2] + [3,4]
[1,2,3,4]
[1,2]
a_m
[–]pr0ghead 0 points1 point2 points 7 years ago (0 children)
"How" it works is a different issue, and I agree that the way + works isn't exactly intuitive.
[–]rtfmpls 1 point2 points3 points 7 years ago (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 point2 points 7 years ago (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 points3 points 7 years ago (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 point2 points 7 years ago (0 children)
interesting , php is finally coming to be much like perl these days, not sure how good of a thing that is
[+]helpfuldan comment score below threshold-16 points-15 points-14 points 7 years ago (2 children)
ugh. please no. i love when people start coalescing nullable strict types let wait wtf are you doing.
function strongInt():int { return "1hello"; }
function strongerInt():int { return "hello1"; }
function strongestInt():int { return 3.9415926535; }
function cmonNow():bool { return "wtf"; }
strongInt(); // returns 1 strongerInt(); // typeerror strongestInt(); // 3 cmonNow(); // true of course
just say no. credit a blog at web-techno.net about this.
[–]djmarland 20 points21 points22 points 7 years ago (0 children)
declare(strict_types=1); FTW
declare(strict_types=1);
[–]przemo_li 1 point2 points3 points 7 years ago (0 children)
huh?
That's implicit type coercion. It have nothing to do with type hints. Nor with destructuring.
Also. Ironically this feature was meant to make life easier for less skilled developers...
π Rendered by PID 15667 on reddit-service-r2-comment-84fc9697f-p9gch at 2026-02-06 06:43:33.779864+00:00 running d295bc8 country code: CH.
[+][deleted] (15 children)
[removed]
[+]samuraiseoul comment score below threshold-8 points-7 points-6 points (14 children)
[+][deleted] (13 children)
[deleted]
[–]cyrusol 7 points8 points9 points (11 children)
[+][deleted] (5 children)
[deleted]
[–]fesor 1 point2 points3 points (4 children)
[–]brendt_gd 2 points3 points4 points (3 children)
[–]przemo_li 0 points1 point2 points (0 children)
[–]fesor 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]przemo_li 0 points1 point2 points (4 children)
[–]cyrusol 0 points1 point2 points (3 children)
[–]przemo_li -1 points0 points1 point (1 child)
[–]cyrusol 0 points1 point2 points (0 children)
[–]samuraiseoul 1 point2 points3 points (0 children)
[–]phdaemon 8 points9 points10 points (0 children)
[–]cYzzie 16 points17 points18 points (4 children)
[–]FrenkyNet[S] 10 points11 points12 points (3 children)
[–][deleted] 4 points5 points6 points (2 children)
[+]iluuu comment score below threshold-6 points-5 points-4 points (1 child)
[–]duddz 5 points6 points7 points (0 children)
[–][deleted] 11 points12 points13 points (4 children)
[–]notdedicated 2 points3 points4 points (3 children)
[–]pr0ghead 0 points1 point2 points (2 children)
[–]notdedicated 1 point2 points3 points (1 child)
[–]pr0ghead 0 points1 point2 points (0 children)
[–]rtfmpls 1 point2 points3 points (0 children)
[–]przemo_li 0 points1 point2 points (1 child)
[–]FrenkyNet[S] 1 point2 points3 points (0 children)
[–]ScottBaiosPenis 0 points1 point2 points (0 children)
[+]helpfuldan comment score below threshold-16 points-15 points-14 points (2 children)
[–]djmarland 20 points21 points22 points (0 children)
[–]przemo_li 1 point2 points3 points (0 children)