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?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityToday 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?
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.
.htaccess
, but I don't understand those well, either.
Jun 28, 2012 at 10:25
$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?
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.
Are there any good use-cases for variable variables?
It depends on your definition of "good".
My personal answer is simply no.
I tried to come up with one just to answer this question, I raked help questions on SO where people are using them, and read a guides and blog posts.
I just cannot find a scenario where it can be argued using a variable variable is more useful or worthwhile than another approach.
All I could think of and found where reasons against their usage.
I even read one article, with grateful and positive comments, which argued how it can make switch statements easier.
Switch statements, "simpler"?
I'd argue that even to someone very new to PHP, an array might arguably be a little trickier to wrap their head around at first, but the learning curve would drop way past that of learning variable variables once they begin trying to use them within code.
It would soon become confusing trying to follow and control the flow of variable variables.
Whereas once learned, an array is quite simple to look at and use.
As I see them, they:
$var = "james";
var name and value are right there.Even the PHP manual has information which, while unintentional, shows variable variables are complicated and potentially "troublesome":
PHP.net wrote:
In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.
As their usage is very uncommon, I would argue that even seasoned developers would suddenly have some learning curve if another dev used this in their code.
Variable variables cannot be used with PHP's Superglobal arrays within functions or class methods.
This is more learning, and careful consideration to work around problems which using variable variables introduce.
For no benefit.
I think they may have been quite useful in older times, when "dinosaurs" roamed the datacentres. and PHP had not been refactored greatly and so much faster with things such as arrays etc.
If variable variables gave us some profound functionality, or even just highly useful, then perhaps using them would be worth the learning.
However, given what they offer, I just do not see the point in ever using them, even if an array is overkill and used some microseconds more, for the sake of code maintainability, readability, sanity preservation, and no difference in functionality, just use an alternative method - every time.