Variable variables: when useful? [duplicate]

2019-01-25 23:28发布

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.

3条回答
劫难
2楼-- · 2019-01-26 00:00

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.

查看更多
▲ chillily
3楼-- · 2019-01-26 00:10

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

查看更多
smile是对你的礼貌
4楼-- · 2019-01-26 00:13

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

查看更多
登录 后发表回答