9

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.

Community
  • 1
  • 1
Slavic
  • 1,852
  • 2
  • 16
  • 25
  • 2
    possible duplicate of [What does $$ mean in PHP?](http://stackoverflow.com/questions/2715654/what-does-mean-in-php) – Charles Jan 21 '12 at 19:22
  • Possible duplicate of [What does $$ (dollar dollar or double dollar) mean in PHP?](https://stackoverflow.com/questions/2715654/what-does-dollar-dollar-or-double-dollar-mean-in-php) – Cœur Jul 10 '18 at 14:19

4 Answers4

17

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.

Erik
  • 3,920
  • 2
  • 25
  • 20
  • Thanks, makes sense. However, if I use it within the context of the the view object, where the only properties are the values to be accessed by the view script, i still tihnk one can freely use it. – Slavic Nov 14 '10 at 20:28
  • 4
    I disagree with this answer. If you are using this in a View object then you're in a limited scope. The $$ is only a problem if you're always working in global scope which you aren't doing if you're using an MVC structure. If your View object really needs some 'reserved' variables that you don't want overridden, name it something like `$_var` so you won't accidentally override it. – Lotus Notes Nov 14 '10 at 20:52
  • 1
    That's why I wrote "generally". Slavic seems to know what he's doing. – Erik Nov 14 '10 at 21:28
4

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.

Crozin
  • 41,538
  • 12
  • 84
  • 134
  • Extract only works for arrays. The reasons I'm using an object is because of the underlying code structure. I don't see a problem in using get_object_vars(); – Slavic Nov 14 '10 at 20:27
  • And probably you want to extract skippingly, so no variables are overwritten. – NikiC Nov 14 '10 at 20:28
1

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.

Webnet
  • 55,814
  • 100
  • 278
  • 454
  • That's because I supply the values to a view script, which accesses much more elegantly values in $value fashion, rather than $this->value fashion. – Slavic Nov 14 '10 at 20:23
-1

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!

  • 3
    Powerful, not powerful, but from the linked question it seems it is just shortcut to (simple form) `eval`. Does it do anything beyond that? – greenoldman Feb 01 '15 at 18:55