开发者

Double dollar sign php

Possible Duplicate:

What does $开发者_运维技巧$ (dollar dollar or double dollar) mean in PHP?

I found myself using this kind of code in one of my controllers:

foreach(get_object_vars($this->view) as $property=>$value){
   $$property = $value;
}

Is there any problem with using the $$property to "localize" view properties into simple $variables?

edit:

I should add that this code is run in the scope of a view-specific method, so there's no problem of overriding local variables. This is to divert some of you guys from pointing out the problem of overriding local variables.


The problem with $$ in PHP is that you create unknown variable names, that may override variable names you already use. It is a source for subtle programming errors, and should generally not be used.


Well, you could just use extract() function. Also that fragment: get_object_vars($this->view) indicates that you should rather have some array to store those variables, not an object.


Nope, that's the way to do it and the only way if I'm not mistaken. Though my question would be to ask why you'd want to do that instead of using the object variable.


The accepted answer is so wrong!!! The double dollar is not a cause for problems, and shouldn't be considered as such! The double dollar syntax is in fact a superb PHP feature that can be useful in many situations!

And if you fear that you might be overriding other variables by using it, then this means you have written poor code! And anyway, this syntax is used by more experienced programmers, so the beginners should avoid it, until feeling more sure about what they are doing.

Some situations where this is really useful is looping through variables based on the value of other variables, dynamic variables, agnostic variables, etc.

Again, let me close by saying that the double dollar is a powerful feature and should be treated as such! With great power comes great responsibility!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜