9

Variable variables seem pretty cool, but I can't think of a scenario where one would actually use them in a production environment. What would such a scenario be? How were they used?

7 Answers 7

7

Its purpose, I guess, is to allow novice programmers to dynamically change data without using "complicated stuff" like composite types (arrays and objects).

I never use them.

4
  • // allow novice programmers to dynamically change data without using "complicated stuff" like composite types (arrays and objects) // It is not complicated once it has been learned.
    – dreftymac
    Nov 26, 2012 at 23:18
  • 1
    I think that's the meaning of the quotes.
    – user151841
    Nov 27, 2012 at 15:19
  • 3
    They're totally useless.
    – frosty
    Oct 14, 2015 at 18:49
  • Since so many pages on Stack Overflow are being routed here, I wish this accepted answer was more scathing. I find variable variables to be more complicated than array/objects. And your IDE is likely to lose track of your dynamically named variables and issue false warnings. Jun 27, 2022 at 0:37
2

A variable variable is essentially an array (map/dictionary). The following are equivalent ideas:

<?php
$foo = array('a' => 1);
$bar = 'a';
echo $foo[$bar]."\n";

$foo_a = 1;
$bar = 'a';
$vv = "foo_$bar";
echo $$vv."\n";
?>

Thus if you wrap your "variable variables" into a parent array, you can do away with them.

I've seen people use variable properties inside classes:

<?php
class Foo
{
  private $a = 1;

  public function __get($key)
  {
    if (isset($this->$key)) return $this->$key;
  }
}

$foo = new Foo();
echo $foo->a;
?>

But again, you could use an array:

<?php
class Foo
{
  private $props = array('a' => 1);

  public function __get($key)
  {
    if (array_key_exists($key, $this->props))
      return $this->props[$key];
  }
}

$foo = new Foo();
echo $foo->a;
?>

And outside classes:

<?php
class Foo
{
  public $a = 1;
}

$foo = new Foo();
$prop = 'a';
echo $foo->{$prop};
?>

So you never "have" to use variable variables or variable properties when writing your own controlled code. My personal preference is to never use variable variables. I occasionally use variable properties, but prefer to use arrays when I'll be accessing data in that way.

1

I found it useful in a single scenario. I was having YouTube API results in JSON format, like this

 $obj->media$title => Video title

So I used it like

$mt = 'media$title';
$obj->$mt ;

So it worked for me here :)

1
  • You could just do $obj->{'media$title'}.
    – Artefacto
    Aug 19, 2010 at 17:19
0

Personally, I use them fairly often. All calls of the following types use variable-variables:

$foo->$bar = 'test';
$foo->$bar();
$bar();

So any time you do a dynamic method/function call, you're using variable-variables...

A common use for this is accessing protected properties via the __get magic method. I've seen the following quite often:

public function __get($name) {
    return isset($this->$name) ? $this->$name : null;
}

Which by definition is using variable variables to provide read-access to the protected members...

I've never directly used the $$var syntax (and don't think I ever will). I have seen it used to access global variables by name global $$name; echo $$name;, but the same thing can be done with the $_GLOBALS[$name] syntax, so that's not a good use-case (not to mention that using global variables is usually seen as bad practice)...

3
  • 1
    Those are not variable variables. Methods are not variables.
    – Artefacto
    Aug 19, 2010 at 16:31
  • Though they can be (kind of) if this go ahead: wiki.php.net/rfc/closures/…
    – Artefacto
    Aug 19, 2010 at 16:33
  • It's a variable-function. You're right. It still operates on the same principal though (where the variable is dereferenced to determine the execution path)...
    – ircmaxell
    Aug 19, 2010 at 16:35
0

Think of it for use in a template system where you are using PHP files and need to set in variables:

function fetch_template($file, $vars){
    $ret = 'File not loaded.';
    if(file_exists(TEMPLATE_PATH.$file)){
        //could do this with extract() but I am showing you
        foreach($vars as $varName => $value){
            ${$varName} = $value;
        }
        ob_start();
        include(TEMPLATE_PATH.$file);
        $ret = ob_get_contents();
        ob_end_clean();
    }
    return $ret;
}

Now assuming you used these variable names in your template, you could call it and pass variables into it for use.

echo fetch_template('hi_there.tpl', array('name'=>'JJ'));

Then in your template:

Hello <?php echo $name; ?>!
3
  • This same thing can be done with extract Aug 19, 2010 at 16:20
  • 2
    And now you have a bug $vars has a key "file". This is why variable variables (and extract, for that matter) are dangerous. Would it be so much harder to use something like $m['var'] in the template?
    – Artefacto
    Aug 19, 2010 at 16:23
  • @Daniel: the code example explicitly says that can be done with extract(). This was an example. @Artefecto: this was not produced to demonstrate security, but instead the use of variable variables. I never advocated passing in variables blindly like anything in the $_GET array.
    – Jage
    Aug 19, 2010 at 16:31
0

I mainly use it to reduce copy-paste in sanitizing get/post data in the begining of a php file: It makes sanitized variables with the proper names:

$fields=array('age','name','gender','email','username');

foreach($fields as $field) {
    if (empty($_REQUEST[$field] === false)
        ${$field} = sanitize($_REQUEST[$field]);
    else
        ${$field} = '';
}

instead of all these lines:

if (empty($_GET['age']) === false) 
    $age= sanitize($_GET['age']);
else
    $age= '';

if (empty($_GET['name']) === false) 
    $name= sanitize($_GET['name']);
else
    $name = '';

if (empty($_GET['gender']) === false) 
    $gender= sanitize($_GET['gender']);
else
    $gender= '';

if (empty($_GET['email']) === false) 
    $email= sanitize($_GET['email']);
else
    $email= '';

if (empty($_GET['username']) === false) 
    $username= sanitize($_GET['username']);
else
    $username= '';

I hope it helps

2
  • Why not just array_map?
    – user151841
    Aug 12, 2017 at 2:05
  • I would not be creating individual variables at all. I'd be pushing these keys (age, name, etc) into my $sanitized array. Jun 27, 2022 at 0:34
0

I know this is an old thread and variable variables are not very used. In fact, it won't surprise me if it's going deprecated in future versions.

But I've found it very useful as an alternative of switch statement, much cleaner, simple and brief solution. Also it seems to perform more efficiently than switch statement.

So this:

class AlterSwitch{

    public static function the_case($option){

        $dirname = dirmane(__DIR__);

        switch ($option){
            case 'root':
                $section = $dirname . '/templates/root.html';
                break;
            case 'head':
                $section = $dirname . '/templates/head.html';
                break;        
            case 'item_cont':
                $section = $dirname . '/templates/item_cont.html';
                break;
            case 'item':
                $section = $dirname . '/templates/item.html';
                break;
    }
    return $section;

}

Can be turned into this:

class AlterSwitch{

    public static function the_case($option){

        $dirname = dirmane(__DIR__);

        $root = $dirname . '/templates/root.html';
        $head = $dirname . '/templates/head.html';
        $item_cont = $dirname . '/templates/item_cont.html';
        $item = $dirname . '/templates/item.html';

        if($$option!==null){
            # Variable variable is the parameter and returns the Variable of this class
            return $$option;
    } 
    return "File not found";  
    }

}


// Instantiate e.g.
// For both classes
// returns $section - $head

AlterSwitch::the_case('head'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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