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

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to