NYCPHP Meetup

NYPHP.org

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

Chris Snyder chsnyder at gmail.com
Fri Jan 31 10:08:15 EST 2014


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20140131/3bf1454f/attachment.html>


More information about the talk mailing list