You are here: Re: Variable variables in classes « PHP Programming Language « IT news, forums, messages
Re: Variable variables in classes

Posted by Steve on 11/20/07 16:08

"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 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.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация