473,800 Members | 2,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1143
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.c omwrote in message
news:13******** ****@corp.super news.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.c omwrote in message
news:13******** ****@corp.super news.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.comwro te in message
news:op******** *******@metalli um.lan...
On Tue, 20 Nov 2007 17:08:14 +0100, Steve <no****@example .comwrote:
>
"MW" <bu**@spandan.c omwrote in message
news:13******** ****@corp.super news.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.comwro te in message
news:op******** *******@metalli um.lan...
On Tue, 20 Nov 2007 17:08:14 +0100, Steve <no****@example .comwrote:
>"MW" <bu**@spandan.c omwrote 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.c om/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
3198
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
6535
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 C. It can cause very ugly errors,like this: epsilon=0 S=0 while epsilon<10: S=S+epsilon
3
2869
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 maintain encapsulation by leaving it inside the loop? Common stuff for examples: class Data;
12
5886
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 these classes. I know there is a pattern called singleton can more or less do such a trick. I am wondering is this the best way to do it (regarding the convenience and safety), as this is such a fundamental thing, I believe most of you have a say...
0
1336
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 inherited classes are all identical. I need to refer to the static variables in each class, and each must maintain its own values for each static variable. Now: when I inherit my base classes, the static variables in all my classes contain...
8
2139
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), and writing the result to disk afterwards. Therefore I have created some classes, e.g. WaveFileIO and AiffFileIO and MP3FileIO and AuFileIO for the In/Out operations.
17
5095
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 guidance appreciated. Regards
1
25703
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 bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
37
5488
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 are the one which runs statically with the defining class. Hence, my understanding is that static variables must be bound to the class defining the variables and shared by children of parent class where the variable is defined. But, please have a...
112
5489
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 that may print some messages. foo(...) { if (!silent)
0
9691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10036
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7582
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.