Re: Shorthand proposal for associative arrays
| From: | Michael Morris | Date: | Fri, 26 Jan 2018 21:39:28 +0000 |
| Subject: | Re: Shorthand proposal for associative arrays | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-101708@lists.php.net to get a copy of this message | ||
Forgot something in the previous post...
On Fri, Jan 26, 2018 at 12:16 PM, Christian Schneider <cschneid@cschneid.com
> wrote:
> Hi there,
> I have a proposal for a shorthand notation of associative arrays borrowed
> from another language:
> :$foo
> would be equivalent to
> 'foo' => $foo
> and would work with array, list or []
>
> Motivation behind it, maybe someone else finds more good uses:
>
> 1) Emulating named parameters with associative arrays like
> html::img([ 'src' => $src, 'alt' => $alt ]);
> could be written as
> html::img([ :$src, :$alt ]);
> which encourages consistent naming of variables and parameters
>
The most similar extant feature to this is compact, but it's not as compact
as this syntax.
$src = 'my.jpg';
$alt = 'My alt';
html::img(compact($src, $alt));
will accomplish the same thing since compact does the reverse of extract -
it pulls the specified variables from the local scope and puts them into an
associative array.