On Mon, Sep 27, 2021 at 11:27 AM Konrad Baumgart <kon...@baumgart.pl> wrote:

> 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
>

I'm generally in favor of this functionality. Unfortunately the syntax
options we have for this are not as nice as in other languages. [=> $x] is
probably the most reasonable choice where array literals are concerned.
https://wiki.php.net/rfc/named_params#shorthand_syntax_for_matching_parameter_and_variable_name
also has some thoughts on this topic, as named params have the same problem.

Regards,
Nikita

Reply via email to