views:

106

answers:

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 variable variables are useful? I'm programming in PHP for several years but i've never used them. For me it looks rather funny than useful.

Can you provide some real life examples about variable variables?

Thanks, fabrik

update:

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

+6  A: 

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

Col. Shrapnel
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
Definitively using variable variables is a sign of a poor design. They are only cool for playing :)
alcuadrado
A: 

Its useful for setting up configuration files dynamically. Check these links for better idea.

http://www.untwistedvortex.com/2010/06/06/php-variable-variables/

Paniyar
There is nothing valuable by the first link and second is down.
Col. Shrapnel
I'd rather use .ini files or arrays instead. Variable variables looks unnecessarily complicated to use.
fabrik
ya i agree with you fabrik.
Paniyar
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.
delnan
A: 

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
http://php.net/extract
Col. Shrapnel
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.
delnan
@delnan: That's my biggest doubt about variable variables: Why create new variables when i can access (array) elements directly?
fabrik
@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.
delnan