Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Today I encountered this little PHP gem called variable variables.

$literal = "Hello";
$vv = "literal";
echo $$vv; // => prints "Hello"

Are there actually any real use-cases for this language feature?

share|improve this question
1  
Like most "obscure tricks" its actual use cases are very limited, there are usually much cleaner ways to implement those use cases and even if the use case matches the trick perfectly, it will be hard to maintain. –  Joachim Sauer Jun 28 '12 at 10:24
1  
@JoachimSauer It's not really obscure, variable variables are at the heart of foreach loops. –  Yannis Rizos Jun 28 '12 at 10:31

2 Answers 2

up vote 13 down vote accepted

Assuming a url of http://example.com/hello/world, and the following .htaccess:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ router.php?/$1 [L]

router.php, an overly simplistic router, would be:

<?php

$request = explode("/", $_SERVER["REQUEST_URI"]);

$controllerClass = $request[0];
$controllerActionMethod = $request[1];

$controller = new $controllerClass();
$controller->$controllerActionMethod(); // hello::world();

?>

That's a pretty standard practical use of variable variables. Neat trick, but you should be extremely careful when using it, over/abusing it will certainly lead to horribly unmaintainable code.

Disclaimer: The code presented in the answer is only intended to illustrate the use of the feature. It does not cover proper design, security, maintainability, or sanity. Do not use.

share|improve this answer
8  
Disclaimer: The code presented in the answer is only intended to illustrate the use of the feature, nothing else. Quality wise it's absolutely crap, do not use. –  Yannis Rizos Jun 28 '12 at 1:34
    
(PHP newbie here) I don't see where exactly you used the variable variables, I only see single dollars. I suspect it's got to do with the .htaccess, but I don't understand those well, either. –  Manishearth Jun 28 '12 at 10:25
3  
@YannisRizos - Your example is using a variable method name, not a variable variable. It's entirely different. You are effectivly calling a method via reflection rather than a variable by reflection. –  Jonno Jun 28 '12 at 11:31
1  
@Jonno Heh, I thought exactly the same when somebody first explained variable variables to me ;) The first "variable", however, refers to the dynamic nature of the (second) variable (the structure), and its method is referenced dynamically, and that's about it. $controller is a variable (the structure), but what it holds is set dynamically, thus a variable variable. Bit of a silly name, ain't it? –  Yannis Rizos Jun 28 '12 at 11:41
1  
@YannisRizos - I would respectfully disagree. A variable variable, is exactly that a variable, which is created at the scope of the currectly created code. What you are doing here is calling a method, who's name is defined by a variable. By that logic, any use of a 'variable' to do anything that... 'varies' is a variable variable. –  Jonno Jun 28 '12 at 13:17

Honestly? In nearly 10 years of PHP experience I've never encountered a problem to which the only solution (or indeed the best solution from a choice) was to use a variable variable. The closest I've got is being able to access constants on an instance of a class such as $classInstance::CLASS_CONSTANT (which has been available since PHP 5.3) and even then I don't normally have much use for it.

I suspect it's one of the many warts that exist in PHP that seemed like a good idea at the time.

So in answer to your question, if there are use cases where it's the best or only approach to a particular problem, I've not seen them.

share|improve this answer
    
Same here - in seven years I worked on and off with PHP I never encountered vv (in production code anyway). –  miku Jun 28 '12 at 13:28
    
Variable variables may seem like a neat trick, but then it will bite you (or someone else) horribly later on when they try to upgrade/maintain the code. Variable variables combined with global scope result in disaster where simply renaming a variable could make your code fall over. It also means that your code cannot be improved by the JIT compiler in HHVM should that ever be an issue. –  Programster Jun 12 at 14:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.