Variable variables in classes
Variable variables in classes
am 20.11.2007 16:29:42 von mw
I wanted to know if it was possible to use the "Variable variable"
syntax to access a public variable inside a class. For example, using
the code below, I am trying to set the value of variable $var_a in the
class Cls from outside the code, but want to be able to use the same
code to assign a value to $var_b. i.e. I don't want the variable's name
defined in the code.
class Cls {
public $var_a;
public $var_b;
}
$x=new Cls;
$y="var_a";
$x->$y="Hello"; // want this to translate to $x->var_a="Hello"
I tried using $x->{$y}="Hello" but that did not work either...
Re: Variable variables in classes
am 20.11.2007 17:08:14 von Steve
"MW" wrote in message
news:13k5vaqt73n2e7@corp.supernews.com...
>I wanted to know if it was possible to use the "Variable variable"
> syntax to access a public variable inside a class. For example, using
> the code below, I am trying to set the value of variable $var_a in the
> class Cls from outside the code, but want to be able to use the same
> code to assign a value to $var_b. i.e. I don't want the variable's name
> defined in the code.
>
> class Cls {
> public $var_a;
> public $var_b;
> }
>
> $x=new Cls;
> $y="var_a";
> $x->$y="Hello"; // want this to translate to $x->var_a="Hello"
>
> I tried using $x->{$y}="Hello" but that did not work either...
try:
$x->$$y = 'hello'
or:
$x->${$y} = 'hello'
then again, i loathe variable variables as they are a *KEY* indication that
basic architecture is severly flawed. i don't know why i just encouraged you
to use them.
Re: Variable variables in classes
am 20.11.2007 17:08:58 von mw
I take it back - the original syntax as described in the code actually
does work. Sorry!
MW wrote:
> I wanted to know if it was possible to use the "Variable variable"
> syntax to access a public variable inside a class. For example, using
> the code below, I am trying to set the value of variable $var_a in the
> class Cls from outside the code, but want to be able to use the same
> code to assign a value to $var_b. i.e. I don't want the variable's name
> defined in the code.
>
> class Cls {
> public $var_a;
> public $var_b;
> }
>
> $x=new Cls;
> $y="var_a";
> $x->$y="Hello"; // want this to translate to $x->var_a="Hello"
>
> I tried using $x->{$y}="Hello" but that did not work either...
Re: Variable variables in classes
am 20.11.2007 18:06:03 von luiheidsgoeroe
On Tue, 20 Nov 2007 17:08:14 +0100, Steve wrote:
>
> "MW" wrote in message
> news:13k5vaqt73n2e7@corp.supernews.com...
>> I wanted to know if it was possible to use the "Variable variable"
>> syntax to access a public variable inside a class. For example, using=
>> the code below, I am trying to set the value of variable $var_a in th=
e
>> class Cls from outside the code, but want to be able to use the same
>> code to assign a value to $var_b. i.e. I don't want the variable's na=
me
>> defined in the code.
>>
>> class Cls {
>> public $var_a;
>> public $var_b;
>> }
>>
>> $x=3Dnew Cls;
>> $y=3D"var_a";
>> $x->$y=3D"Hello"; // want this to translate to $x->var_a=3D"Hello"
>>
>> I tried using $x->{$y}=3D"Hello" but that did not work either...
>
> try:
> $x->$$y =3D 'hello'
> or:
> $x->${$y} =3D 'hello'
Which would require you have a $var_a set outside the class:
$y=3D"var_a";
$var_a =3D "foo";
This: $x->$$y =3D 'hello'
Is this: $x->$var_a =3D 'hello'
Is this: $x->foo =3D 'hello'
As the OP allready stated, his original code just plainly works.
-- =
Rik Wasmus
Re: Variable variables in classes
am 20.11.2007 23:03:04 von Steve
"Rik Wasmus" wrote in message
news:op.t13ogdmp5bnjuv@metallium.lan...
On Tue, 20 Nov 2007 17:08:14 +0100, Steve wrote:
>
> "MW" wrote in message
> news:13k5vaqt73n2e7@corp.supernews.com...
>> I wanted to know if it was possible to use the "Variable variable"
>> syntax to access a public variable inside a class. For example, using
>> the code below, I am trying to set the value of variable $var_a in the
>> class Cls from outside the code, but want to be able to use the same
>> code to assign a value to $var_b. i.e. I don't want the variable's name
>> defined in the code.
>>
>> class Cls {
>> public $var_a;
>> public $var_b;
>> }
>>
>> $x=new Cls;
>> $y="var_a";
>> $x->$y="Hello"; // want this to translate to $x->var_a="Hello"
>>
>> I tried using $x->{$y}="Hello" but that did not work either...
>
> try:
> $x->$$y = 'hello'
> or:
> $x->${$y} = 'hello'
=============
> Which would require you have a $var_a set outside the class:
>
> $y="var_a";
> $var_a = "foo";
which is the exact thing his example shows.
> This: $x->$$y = 'hello'
> Is this: $x->$var_a = 'hello'
> Is this: $x->foo = 'hello'
> As the OP allready stated, his original code just plainly works.
right...but please note, i responded before he remarked that his code *did*
work. so, to me, it makes no difference.
btw, rik...PLEASE set your newreader to PROPERLY mark-up/indent your
replies. it's a pain in the ass to respond to you since i have to manually
put in > for everything you type...just so everyone can tell where my
response goes in all that mess.
thanks.
Re: Variable variables in classes
am 21.11.2007 01:44:32 von luiheidsgoeroe
On Tue, 20 Nov 2007 23:03:04 +0100, Steve wrote:
> "Rik Wasmus" wrote in message
> news:op.t13ogdmp5bnjuv@metallium.lan...
> On Tue, 20 Nov 2007 17:08:14 +0100, Steve wrote:
>> "MW" wrote in message
>>> class Cls {
>>> public $var_a;
>>> public $var_b;
>>> }
>>>
>>> $x=3Dnew Cls;
>>> $y=3D"var_a";
>>> $x->$y=3D"Hello"; // want this to translate to $x->var_a=3D"Hello"
>>>
>>> I tried using $x->{$y}=3D"Hello" but that did not work either...
>>
>> try:
>> $x->$$y =3D 'hello'
>> or:
>> $x->${$y} =3D 'hello'
> =============3D
>
>> Which would require you have a $var_a set outside the class:
>>
>> $y=3D"var_a";
>> $var_a =3D "foo";
>
> which is the exact thing his example shows.
No, it isn't
>> This: $x->$$y =3D 'hello'
>> Is this: $x->$var_a =3D 'hello'
>> Is this: $x->foo =3D 'hello'
>
>> As the OP allready stated, his original code just plainly works.
>
> right...but please note, i responded before he remarked that his code =
=
> *did*
> work. so, to me, it makes no difference.
Euhm, you mean you were just wildly guessing? The correct answer should =
=
have been 'it does work'.
> btw, rik...PLEASE set your newreader to PROPERLY mark-up/indent your
> replies. it's a pain in the ass to respond to you since i have to =
> manually
> put in > for everything you type...just so everyone can tell where my
> response goes in all that mess.
If your newsreader cannot cope with format=3Dflowed (From the header: =
'Content-Type: text/plain; format=3Dflowed; delsp=3Dyes; charset=3Diso-8=
859-15') =
it's either ancient or just plainly sucks. Please upgrade (OEQuotefix? =
, a quick test here shows =
it =
whips MSOE rightly into shape).
Please look at the source or my post, and notice that my message is =
actually fullly compliant. Check the relevant RFC's.
-- =
Rik Wasmus
Re: Variable variables in classes
am 21.11.2007 18:20:49 von Michael Fesser
..oO(Steve)
>btw, rik...PLEASE set your newreader to PROPERLY mark-up/indent your
>replies. it's a pain in the ass to respond to you since i have to manually
>put in > for everything you type...just so everyone can tell where my
>response goes in all that mess.
As already said earlier - it's your own OE which causes the problem
(quoted-printable bug). What about changing to a real newsreader?
Micha