Variable variables: when useful? [duplicate]

旧街凉风 提交于 2019-11-28 01:41:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!