|
Posted by Rik Wasmus on 11/20/07 17:06
On Tue, 20 Nov 2007 17:08:14 +0100, Steve <no.one@example.com> wrote:
>
> "MW" <bulk@spandan.com> 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
[Back to original message]
|