3

Possible Duplicate:
What's an actual use of variable variables?

OK, this question may look a little bit point-whoreish, but I'd really like to know: when are variable variables useful? I haved programmed in PHP for several years, but I've never used them. For me it looks rather funny than useful.

What are some real life examples of variable variables?

Update:

I'm really sorry about your votes. The linked 'duplicate' may be really a dupe except one thing: the examples listed there show me why not to use variable variables.

Community
  • 1
  • 1
fabrik
  • 14,094
  • 8
  • 55
  • 71

3 Answers3

7

Never. This feature should be banned from the language as well as its other children diseases such as register globals, magic quotes, etc.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • I've never met a problem where i found this useful. Arrays are always looks more simple and useful than juggling with variable names without a good reason. Should i think this function is a fail of this language? – fabrik Aug 27 '10 at 07:44
  • Definitively using variable variables is a sign of a poor design. They are only cool for playing :) – alcuadrado Aug 27 '10 at 09:25
  • This would be the first time I've ever downvoted a Col. Shrapnel post. But I disagree with the word "never" - variable variables are useful for templating systems. – Lotus Notes Jun 17 '11 at 18:59
1

I have seen it used in some MVC frameworks to initialize variables in the View object.

foreach ($this->vars as $key => $value)
{
    // some test would be here so there are no conflicts
    $$key = $value;
}

You can then access the the variabels in templates.

I'm not saying this is a good use though. Probably a case of poor design.

Richard Knop
  • 81,041
  • 149
  • 392
  • 552
  • Extract is only marginally better. At least you can easily stop it from overwriting existing variables. But since you already have an associative array when you can use extract, you should just use that. –  Aug 27 '10 at 09:48
  • @delnan: That's my biggest doubt about variable variables: Why create new variables when i can access (array) elements directly? – fabrik Aug 27 '10 at 09:53
  • @fabrik that's exactly what everyone "dislikes" (to put it mildly) about variable variables: Associative arrays literally provide the same functionality (actually, more - keys are not required to form valid identifiers), but without all the dangers. –  Aug 27 '10 at 09:59
  • `extract` does the exact same thing as the foreach loop mentioned by @Richard Knop. – Lotus Notes Jun 22 '11 at 18:30
0

It's useful for setting up configuration files dynamically. Check PHP and Variable Variables for a better idea.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ArK
  • 20,698
  • 67
  • 109
  • 136
  • There is nothing valuable by the first link and second is down. – Your Common Sense Aug 27 '10 at 07:39
  • I'd rather use .ini files or arrays instead. Variable variables looks unnecessarily complicated to use. – fabrik Aug 27 '10 at 07:46
  • 1
    I would **never even think about** injecting abritary variables from a file into my code... WTF is wrong with an associative array? I consider downvoting, give me a good reason not to. –  Aug 27 '10 at 08:18