Variable variables (or their official name – pointed out by Ionut Stan in the comments) or “dynamic variables” (how I like to call them) is the name of a feature of the PHP programming language which allows a programmer to define a variable that has its name given by the content of another variable. Like such:
$foo = 'bar'; /* * declare a variable called bar into the * current scope and initialise it with Hello world */ $$foo = 'Hello world'; echo $bar; // will echo Hello world
If find this really useless and very annoying, because a typo such as a double dollar sign in front of a variable can result in some very hard to track bugs. But this isn’t just another strange feature that made its way into the trunk. This is very, very well implemented and some programmers strived a lot to make it work under all possible circumstances. It works so well that you can use strings that cannot be normally used to represent variable names, such as !@#$%^&*()_+-=/*. Really, it works!
$foo = '!@#$%^&*()_+-=/*'; $$foo = 'Hello world'; $variables = get_defined_vars(); echo $variables['!@#$%^&*()_+-=/*']; /** * will echo Hello world - the content of a variable called !@#$%^&*()_+-=/* */
Further more, it even works with objects:
class Example {
/**
* sample attribute
*
* @var string
*/
private $value;
/**
* default constructor
*
* @param $_value
*/
public function __construct( $_value ) {
$this->value = (string) $_value;
}
/**
* returns a string representation
* of this object
*
* @return string
*/
public function __toString() {
return $this->value;
}
}
$foo = new Example( 'bar' );
$$foo = 'Hello world';
echo $bar; // will echo Hello world
I think this one of those features reserved for the most gurus of the gurus, because after 5 years experience with PHP and a Zend Certification, I haven’t yet grasped the logic behind this…
Example:
$class_name = 'SomeClassName'; $object = new $class_name;dynamic instantiation
Dynamic functions and dynamic instantiation are *not* the same as dynamic variables!
Yes, Tudor, when using dynamic variables you can get bugs very hard to debug. But even if you have no bugs, it’s very tricky to refactor the code or even to understand it.
I think that there are very very few cases when you really need dynamic variables. I even can bet that you can do the same thing using associative arrays. Btw, I suppose that internally PHP stores variables in associative arrays… that could explain all the logic…
I know this feature by the name “variable variables” but I guess dynamic variables makes sense too.
I didn’t get what logic you hadn’t yet understood. The purpose of these dynamic variables?
Ionut: Yes! I don’t get it why did anyone strive so much to implement this feature. I find it quite useless. And yes, you are right, they are called “variable variables” – this is the official name
I’ve changed the name above.
The only useful purpose that comes to mind is obfuscation. I’ve seen some nice examples of obfuscation using variable variables. But, you’re right about striving to implement such a thing. I don’t remember to have ever used them.
Tudor it’s not useless, the following example code creates variables from a form’s post fields:
foreach ($_POST as $key => $value): $allowed = array('username','password','repeat','email','why'); if (in_array($key,$allowed)): $$key = $value; endif; endforeach;Yes, I agree that there are thousands of nifty pieces of code out there – like the one you’ve posted – that make good use of this feature, but they are somewhat “artificial” and partially reserved for the more seasoned programmers. And that double dollar sign is very easy to miss. And while that code is clear to us, imagine how it would look to a junior programmer.
You could also do it like this, without using variable variables:
$allowed = array('username','password','repeat','email','why'); foreach( $_POST as $key => $value ) { if( in_array( $key, $allowed ) ) { extract( array( $key => $value ) ); } }I agree it doesn’t have that Perl hacker style your script has, but it’s easier to understand.
Ups!!!!!
)
Mea culpa!
Example Snippet (very simple, and useful example):
$offset = $value; // $company->name = "Foo, Inc." } public function __get($offset) { return $this->$offset; // echo $company->name } }Also, if you wrap your variable inside of curly braces, it achieves the same effect, while making your code a little easier to understand: This is especially important if you use a variable in the middle of a string literal:
echo $this->{$method}($param1);But, you do raise a very good point, in that people need to pay very close attention to their use of variable-variables as the bugs are more difficult to track.
[...] came to the conclusion that PHP is the programing language with the weirdest features. After the variable variables mess, that allows to you to name your variables stuff like !@#$%^&*()_+= and not be able to [...]