Proposal: Shorthand initialization and destructuring of associative arrays

From: Date: Mon, 27 Sep 2021 09:27:02 +0000
Subject: Proposal: Shorthand initialization and destructuring of associative arrays
Groups: php.internals 
Request: Send a blank email to internals+get-116177@lists.php.net to get a copy of this message
Hi everyone, I'd like to propose 2 syntactic sugars: $array = [ => $data]; // the same as $array = ['data' => $data] and [ => $data] = $array; // the same as ['data' => $data] = $array My biggest use-case for this would be conveniently returning multiple things from a function, like: function getDataForDashboard() { … return [ => $dailyAggregations, => $weeklyAggregations]; } [ => $dailyAggregations, => $weeklyAggregations] = getDataForDashboard(); Similar effects can be achieved with compact()/extract() functions, but I dislike those functions because they make it hard to find usages of variables. Using numerical arrays instead makes the code less readable and more error-prone for me. Using ['dailyAggregations' => $dailyAggregations,…] would force you to split code into multiple lines and negatively affect readability. I was recently developing with js/ts and I liked the ease of returning multiple items from a function as an object, while still preserving their name. I have spare time this October, so I would happily get into php interpreter by developing this. I'm looking forward for your feedback, Konrad Baumgart

« previous php.internals (#116177) next »