Re: Shorthand proposal for associative arrays

From: Date: Sat, 27 Jan 2018 23:29:38 +0000
Subject: Re: Shorthand proposal for associative arrays
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-101714@lists.php.net to get a copy of this message
On Fri, Jan 26, 2018 at 4:49 PM, David Rodrigues <david.proweb@gmail.com> wrote: > Sorry. I mean "compact()" instead of extract() (it basically does the > oposite hehe), I confused everything in a hurry. Sorry. > > So the real example is: html::img(compact('src', 'alt')); > > 2018-01-26 19:39 GMT-02:00 Michael Morris <tendoaki@gmail.com>: > >> 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. >> > I see nothing in this proposal that compact doesn't already do quite well. -1

« previous php.internals (#101714) next »