Page 1 of 3
Variable variables
Posted: Sat Aug 28, 2010 3:24 pm
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
Re: Variable variables
Posted: Sat Aug 28, 2010 6:15 pm
by Jonah Bron
I'm torn between saying that that feature seems pointless now or saying it later.
Re: Variable variables
Posted: Sat Aug 28, 2010 6:59 pm
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.
Re: Variable variables
Posted: Sun Aug 29, 2010 2:23 pm
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.
Re: Variable variables
Posted: Sun Aug 29, 2010 8:40 pm
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
Re: Variable variables
Posted: Mon Aug 30, 2010 5:08 am
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.
Re: Variable variables
Posted: Mon Aug 30, 2010 8:53 am
by alex.barylski
I'm missing something but why couldn't you simply set the properties as you would like any object?
Cheers,
Alex
Re: Variable variables
Posted: Mon Aug 30, 2010 12:38 pm
by Ragnis
Because theese variables names are dynamic.
Re: Variable variables
Posted: Mon Aug 30, 2010 12:46 pm
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.

Re: Variable variables
Posted: Mon Aug 30, 2010 1:29 pm
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.

Code: Select all
$obj = new BadClass_Class1;
$arr = range(1, 152);
foreach ($arr as $elt) {
$obj->{'var' . $elt} = $elt;
}
Re: Variable variables
Posted: Mon Aug 30, 2010 1:33 pm
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
Re: Variable variables
Posted: Mon Aug 30, 2010 1:44 pm
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.
Re: Variable variables
Posted: Mon Aug 30, 2010 2:35 pm
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
Re: Variable variables
Posted: Tue Aug 31, 2010 10:19 am
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?
Re: Variable variables
Posted: Thu Sep 02, 2010 12:07 pm
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).