NYCPHP Meetup

NYPHP.org

[nycphp-talk] Variable composition: ${'foo'.$i}

Jeff Slutz jeff at jeffslutz.com
Fri Jan 31 11:15:28 EST 2014


In the case you mention it's also about as easy to read/understand as it
will ever be-- the contents of the array are right there.  I find it gets
trickier when the contents of that array are coming in from outside the
function or method.

Definitely still useful, but gets harder to track.

--
Jeff Slutz
JSLEUTH LLC
2105 N Fork Drive
Lafayette, CO 80026
c. 970.443.9390
jeff at jeffslutz.com


On Fri, Jan 31, 2014 at 8:08 AM, Chris Snyder <chsnyder at gmail.com> wrote:

> On Thu, Jan 30, 2014 at 12:12 PM, Jerry B. Altzman <jbaltz at altzman.com>wrote:
>
>>
>> 'Variable variables' aren't pure evil; they just provide a level of
>> indirection that allows for fine-grained loss of control at runtime.
>>
>
> Not only are they not evil, they're necessary for a lot of Don't Repeat
> Yourself optimizations.
>
> I use this pattern all the time:
>
> foreach( array('pages', 'posts', 'comments') AS $collection ) {
>   ${$collection} = $model->load( $collection );
>   // do more stuff with ${$collection} here
>   $template->assign( ${$collection}, $collection );
> }
>
> It's a little bit harder to read until you get used to it, but because
> it's DRY it's much easier to manage over time.
>
>
> _______________________________________________
> New York PHP User Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> http://www.nyphp.org/show-participation
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20140131/d1971646/attachment.html>


More information about the talk mailing list