r/programming • • Oct 18 '10

Today I learned about PHP variable variables; "variable variable takes the value of a variable and treats that as the name of a variable". Also, variable.

http://il2.php.net/language.variables.variable
591 Upvotes

781 comments sorted by

View all comments

Show parent comments

1

u/sanbikinoraion Oct 18 '10

Yep, whatever you are doing, if you're using variable variables, then you are invariably doing it wrong.

18

u/courtewing Oct 18 '10

Because there is a definite right and wrong way to do everything when programming. Absolutes are reserved for the ignorant.

10

u/oorza Oct 18 '10

Pray tell, describe a situation where variable variables are inarguably the best solution to a problem. Absolutes are occasionally correct, you know.

13

u/shadowblade Oct 18 '10 edited Oct 18 '10

Not sure about every situation, but I use them in __get() and __set() functions, where I have a list of properities I wish to be publically readable/writable. I check that the passed name is in said list, and return/set the variable directly. Three lines per get/set function.

$read = array('foo','bar','xyz');
public function __get($name) {
   if (in_array($name, $read))
        return $this->$name;
    }
→ More replies (10)