473,395 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Variable variables in classes

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...
Nov 20 '07 #1
6 1129
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...
Nov 20 '07 #2

"MW" <bu**@spandan.comwrote in message
news:13************@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.
Nov 20 '07 #3
On Tue, 20 Nov 2007 17:08:14 +0100, Steve <no****@example.comwrote:
>
"MW" <bu**@spandan.comwrote in message
news:13************@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";

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.
--
Rik Wasmus
Nov 20 '07 #4

"Rik Wasmus" <lu************@hotmail.comwrote in message
news:op***************@metallium.lan...
On Tue, 20 Nov 2007 17:08:14 +0100, Steve <no****@example.comwrote:
>
"MW" <bu**@spandan.comwrote in message
news:13************@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.
Nov 20 '07 #5
On Tue, 20 Nov 2007 23:03:04 +0100, Steve <no****@example.comwrote:
"Rik Wasmus" <lu************@hotmail.comwrote in message
news:op***************@metallium.lan...
On Tue, 20 Nov 2007 17:08:14 +0100, Steve <no****@example.comwrote:
>"MW" <bu**@spandan.comwrote in message
>>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.
No, it isn't
>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.
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=flowed (From the header:
'Content-Type: text/plain; format=flowed; delsp=yes; charset=iso-8859-15')
it's either ancient or just plainly sucks. Please upgrade (OEQuotefix?
<http://www.snapfiles.com/get/oequotefix.html>, 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
Nov 21 '07 #6
..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
Nov 21 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
83
by: Alexander Zatvornitskiy | last post by:
Hello All! I'am novice in python, and I find one very bad thing (from my point of view) in language. There is no keyword or syntax to declare variable, like 'var' in Pascal, or special syntax in...
3
by: Thomas Matthews | last post by:
Hi, While coding programs, I cam about a conundrum regarding variables defined in an iterative loop. The issue is whether it is more efficient to factor the definition out of the loop or...
12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
0
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
8
by: Michael | last post by:
Hi, I think my problem deals with class casting and inheritance. I want to deal with various Audio Formats, reading into memory for modification, working with it (done by different classes),...
17
by: Control Freq | last post by:
Hi, Not sure if this is the right NG for this, but, is there a convention for the variable names of a Session variable? I am using .NET 2.0 in C#. I am new to all this .NET stuff, So, any...
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.