Variable variables

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Variable variables

Post by alex.barylski »

NOTE: I could have swore I posted this earlier but maybe I didn't. If I did and it was relocated I appologize in advance :)

So the subject, variable variables. I Googled it to see how or what others might be using it for, but I have yet to find anything that couldn't be done more elgantly using normal variables or arrays. So my question is this, what practical purpose does this feature serve PHP developers? Have you found a neat use for this obscure feature?

Cheers,
Alex
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Variable variables

Post by Jonah Bron »

I'm torn between saying that that feature seems pointless now or saying it later.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Variable variables

Post by Benjamin »

I've used it before. There are times when you do need it. I'll try to find an example if I can remember where I used it last.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Variable variables

Post by josh »

PCSpectra wrote: I have yet to find anything that couldn't be done more elgantly using normal variables or arrays. So my question is this, what practical purpose does this feature serve PHP developers? Have you found a neat use for this obscure feature?
Sometimes the code we are extending isn't as elegant as we'd like, so inelegant solutions are needed to work with it.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Variable variables

Post by alex.barylski »

Sometimes the code we are extending isn't as elegant as we'd like, so inelegant solutions are needed to work with it.
I have worked in PHP for almost 10 years in multiple industries from fashion, to ecommerce to aerospace. Never once have I had to absolutely had to use variable variables. I can't fathom a problem better solved using variable variables than normal arrays, which is why I think an actual example would serve me better.



Cheers,
Alex
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Variable variables

Post by josh »

Code: Select all

class1
{
  var $var1;
  var $var2;
  var $var3;
  var $var4;
  var $var5;
  var $var6;
  var $var7;
  var $var8;
 // snip
  var $var150;
  var $var151;
  var $var152;
 // etc
}
How would you extend this class and set these variables? We're assuming this class is not in your namespace, not in your control. You can't decide to make changes to the class but you can't make rent unless you extend the class. So how do you complete your project? If you don't say variable variables then you are trolling, lol.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Variable variables

Post by alex.barylski »

I'm missing something but why couldn't you simply set the properties as you would like any object?

Cheers,
Alex
User avatar
Ragnis
Forum Commoner
Posts: 31
Joined: Thu Nov 13, 2008 12:35 pm
Location: Saaremaa, Estonia, Europe, Asia, Planet Earth, The Solar System, Milky way.

Re: Variable variables

Post by Ragnis »

Because theese variables names are dynamic.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Variable variables

Post by AbraCadaver »

josh wrote:

Code: Select all

class1
{
  var $var1;
  var $var2;
  var $var3;
  var $var4;
  var $var5;
  var $var6;
  var $var7;
  var $var8;
 // snip
  var $var150;
  var $var151;
  var $var152;
 // etc
}
How would you extend this class and set these variables? We're assuming this class is not in your namespace, not in your control. You can't decide to make changes to the class but you can't make rent unless you extend the class. So how do you complete your project? If you don't say variable variables then you are trolling, lol.
I'm not sure I get it, but show me how you would do it with variable variables and I'll see if I can show you how to do it without. :D
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Variable variables

Post by Weirdan »

AbraCadaver wrote:I'm not sure I get it, but show me how you would do it with variable variables and I'll see if I can show you how to do it without. :D

Code: Select all

$obj = new BadClass_Class1;
$arr = range(1, 152);
foreach ($arr as $elt) {
   $obj->{'var' . $elt} = $elt;
}
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Variable variables

Post by alex.barylski »

Because theese variables names are dynamic.
OK I must be really tired because I still don't follow. :p

What difference would it make whether the variables are dynamic? From what i remember public properties are iterable as an associative array, so you could iterate each and I think assign values like:

Code: Select all

class TestTemp{
  var $var1;
  var $var2;
  var $var3;
  var $var4;
  var $var5;
  var $var6;
  var $var7;
  var $var8;
}

$temp = new TestTemp();
foreach($temp as $name => $value){
  $temp->{"$name"} = 'ALEX WAS HERE';
}
How would you extend this class and set these variables? We're assuming this class is not in your namespace, not in your control. You can't decide to make changes to the class but you can't make rent unless you extend the class. So how do you complete your project? If you don't say variable variables then you are trolling, lol.
If assignment of dynamic values is the only problem, you could extend the class and initialize the properties in the child class ctor using the technique above. I doubt this works on private or protected, so maybe variable variables have an advantage there?

Cheers,
Alex
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Variable variables

Post by AbraCadaver »

Ragnis wrote:Because theese variables names are dynamic.
If you do this: var $var1; then it's not dynamic (at least the name isn't). I see Weirdan's post but that seems to only make sense in that contrived case.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Variable variables

Post by alex.barylski »

I understood the properties to be statically declared but because of incredible number of property values, assigning them one by one was not acceptable. To be honest I have no idea what Josh was getting at, but I assume this anyway.

Cheers,
Alex
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Variable variables

Post by Bill H »

Code: Select all

while ($i < $c)
{    $i++;
     $Xs = sprintf("Val%02d",$i);
     if ($Row[$Xs] < 0)  echo "We did this";
     else                echo "We did that";
}
Is there some way to do that with a variable variable?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Variable variables

Post by josh »

var1, var2, var3 isn't the only pattern where this feature is useful. What if was varA, varB, varC .... or what if you wanted to call one of many methods that differed by a predictable pattern like this? We have other things besides 'for' loops in which a variable variable, or variable function, or variable class name could be used inside of. Would still love to see the example of setting 150 variables without using variable variables. Even the foreach loop would require using them as far as I can see. So you use a foreach loop..

Code: Select all

foreach($this as $variableName => $variableValue )
{
    // now what  ?
}
I don't see what there is not to get, PCSpectra, from how you re-phrased my point you get it just fine. Maybe you have a factory method that needs to accept as an argument a class name that will be instantiated, for that there is variable class names. There is a use case for all this stuff, like I always say programmer's don't just generally build stuff to confuse people. Each feature of the language has a useful application.

Of course you wouldn't write code willingly that uses them. Its a tool for a certain scenario, when you have quite a few different variables you need to set that are named according to a predictable pattern. Its for when the code you're extending is crap (or just not easy to extend).
Post Reply