Re: Shorthand proposal for associative arrays

From: Date: Fri, 26 Jan 2018 21:49:10 +0000
Subject: Re: Shorthand proposal for associative arrays
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-101709@lists.php.net to get a copy of this message
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. > -- David Rodrigues

« previous php.internals (#101709) next »