问题
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.
回答1:
Never. This feature should be banned from the language as well as its other children diseases such as register globals, magic quotes, etc.
回答2:
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.
回答3:
It's useful for setting up configuration files dynamically. Check PHP and Variable Variables for a better idea.
来源:https://stackoverflow.com/questions/3582043/variable-variables-when-useful