4 ms·
I hate how to this day PHP still lacks of an obvious way to write object literals, instead forcing you to either cast arrays, which is verbose and ugly, or use
by Mahn 14y ago
I hate how to this day PHP still lacks of an obvious way to write object literals, instead forcing you to either cast arrays, which is verbose and ugly, or use stdClasses, which personally I do but damn are they a pain to use and maintaining for nested structures. This reason alone is what would make me switch to nodejs next time I start something.
- yogo 14y agoThere has been support for object literals for quite some time now (https://wiki.php.net/rfc/objectarrayliterals https://wiki.php.net/rfc/objectarrayliterals) but maybe just not enough. It would definitely be a welcome addition to the language. At least we no longer need to write array() for arrays lol.
- Mahn 14y agoOnly array literals were implemented though, not objects. That was long overdue, but in the age of JSON we live in it's not enough. For instance, the following in Javascript: var myObject = { memberOne: 'some string value', memberTwo: { subMemberOne: 42, subMemberTwo: 48 }, memberThree: false }; Has this as the closest equivalent in PHP right now: $myObject = (object) [ "memberOne" => 'some string value', "memberTwo" => (object) [ "subMemberOne": 42, "subMemberTwo": 48 ], "memberThree" => false ]; It's verbose, really, I would very much prefer a native solution that doesn't enforce casting from array and quotation around property names.
- yogo 14y agoI know, note what I said "...maybe just not enough. It would definitely be a welcome addition to the language" People have proposed it and commented on it but it does not get enough support (votes) from senior devs to get included. Maybe with more requests it will get included finally.