473,385 Members | 1,355 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,385 software developers and data experts.

Variable Variables in Classes

class example {
var $Stage;

function example() {
$this->Stage = 'init';

$x = ???????;

echo $$x;
}
}

What do I put in $x so that $$x gives me "init"? 'Stage' doesn't work,
nor does 'this->Stage'.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
May 17 '06 #1
10 1439
It looks like in this particular case, $this->$x should be sufficient.
Otherwise, you may have to split up x into $x_object and $x_name
(exploding by -> should be sufficient) and calling $x_object->$x_name.
What you currently have, however, cannot be done.

May 17 '06 #2
Rik
Alan Little wrote:
class example {
var $Stage;

function example() {
$this->Stage = 'init';

$x = ???????;

echo $$x;
}
}

What do I put in $x so that $$x gives me "init"? 'Stage' doesn't work,
nor does 'this->Stage'.

Why use the $$ syntax?

<?php

class test{
var $string;
function text(){
$this->string = "This works";
$check = 'string';
echo $this->$check;
}
}
$a = new test();
$a->text();
?>

Grtz,
--
Rik Wasmus
May 17 '06 #3
Carved in mystic runes upon the very living rock, the last words of
Ambush Commander of comp.lang.php make plain:
It looks like in this particular case, $this->$x should be sufficient.
Thanks, but no.
Otherwise, you may have to split up x into $x_object and $x_name
(exploding by -> should be sufficient) and calling $x_object->$x_name.
What you currently have, however, cannot be done.


I'm not sure what you mean. What I'm trying to do is parse variables out
of a string and substitute them with the value. $x (in this example)
would hold the variable name extracted from the string. I'm trying to
figure out how to get to the value of that variable, from there.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
May 17 '06 #4
Rik
Alan Little wrote:
Carved in mystic runes upon the very living rock, the last words of
Ambush Commander of comp.lang.php make plain:
It looks like in this particular case, $this->$x should be
sufficient.


Thanks, but no.
Otherwise, you may have to split up x into $x_object and $x_name
(exploding by -> should be sufficient) and calling
$x_object->$x_name. What you currently have, however, cannot be done.


I'm not sure what you mean.
What I'm trying to do is parse variables
out of a string and substitute them with the value. $x (in this
example) would hold the variable name extracted from the string. I'm
trying to figure out how to get to the value of that variable, from
there.


That is exactly what it does.
$x = 'string'

$$x = $this->$x;
equals:
$string = $this->string;

$x is the variable name, $variable_name holds the value......

I don't get the problem?

What is the exact reason you're trying to accomplish? Maybe if you elaborate
some more, we get the picture. Why is it this doesn't suffice?

Grtz,
--
Rik Wasmus
May 17 '06 #5
Carved in mystic runes upon the very living rock, the last words of Rik
of comp.lang.php make plain:
Alan Little wrote:
Carved in mystic runes upon the very living rock, the last words of
Ambush Commander of comp.lang.php make plain:
It looks like in this particular case, $this->$x should be
sufficient.


Thanks, but no.
Otherwise, you may have to split up x into $x_object and $x_name
(exploding by -> should be sufficient) and calling
$x_object->$x_name. What you currently have, however, cannot be
done.


I'm not sure what you mean.
What I'm trying to do is parse variables
out of a string and substitute them with the value. $x (in this
example) would hold the variable name extracted from the string. I'm
trying to figure out how to get to the value of that variable, from
there.


That is exactly what it does.
$x = 'string'

$$x = $this->$x;
equals:
$string = $this->string;

$x is the variable name, $variable_name holds the value......

I don't get the problem?

What is the exact reason you're trying to accomplish? Maybe if you
elaborate some more, we get the picture. Why is it this doesn't
suffice?


For example:

$this->animal = 'lamb';
$this->string = 'Mary had a little {{animal}}';

The goal is to extract 'animal' from the string (no problem), convert it
to 'lamb' and replace it in the string (again, no problem). The question
is, if I have 'animal', how do I get 'lamb'? In global space, I would
simply have 'animal' in a variable, and use $$var to get 'lamb'.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
May 17 '06 #6
Carved in mystic runes upon the very living rock, the last words of Rik
of comp.lang.php make plain:
Alan Little wrote:
class example {
var $Stage;

function example() {
$this->Stage = 'init';

$x = ???????;

echo $$x;
}
}

What do I put in $x so that $$x gives me "init"? 'Stage' doesn't work,
nor does 'this->Stage'.


Why use the $$ syntax?

<?php

class test{
var $string;
function text(){
$this->string = "This works";
$check = 'string';
echo $this->$check;


Ah, yes that works. Thank you.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
May 17 '06 #7
"Alan Little" <al**@n-o-s-p-a-m-phorm.com> wrote in message
news:Xn**************************@216.196.97.131.. .
Carved in mystic runes upon the very living rock, the last words of Rik
of comp.lang.php make plain:
Alan Little wrote:
Carved in mystic runes upon the very living rock, the last words of
Ambush Commander of comp.lang.php make plain:

It looks like in this particular case, $this->$x should be
sufficient.

Thanks, but no.

Otherwise, you may have to split up x into $x_object and $x_name
(exploding by -> should be sufficient) and calling
$x_object->$x_name. What you currently have, however, cannot be
done.

I'm not sure what you mean.
What I'm trying to do is parse variables
out of a string and substitute them with the value. $x (in this
example) would hold the variable name extracted from the string. I'm
trying to figure out how to get to the value of that variable, from
there.


That is exactly what it does.
$x = 'string'

$$x = $this->$x;
equals:
$string = $this->string;

$x is the variable name, $variable_name holds the value......

I don't get the problem?

What is the exact reason you're trying to accomplish? Maybe if you
elaborate some more, we get the picture. Why is it this doesn't
suffice?


For example:

$this->animal = 'lamb';
$this->string = 'Mary had a little {{animal}}';

The goal is to extract 'animal' from the string (no problem), convert it
to 'lamb' and replace it in the string (again, no problem). The question
is, if I have 'animal', how do I get 'lamb'? In global space, I would
simply have 'animal' in a variable, and use $$var to get 'lamb'.

So again, wouldn't this work:

$var = 'animal';
$this->$var; //this is translated to $this->animal, which in turn gives you
'lamb' .
--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
May 18 '06 #8
Carved in mystic runes upon the very living rock, the last words of
Kimmo Laine of comp.lang.php make plain:
"Alan Little" <al**@n-o-s-p-a-m-phorm.com> wrote in message
news:Xn**************************@216.196.97.131.. .
Carved in mystic runes upon the very living rock, the last words of
Rik of comp.lang.php make plain:
Alan Little wrote:
Carved in mystic runes upon the very living rock, the last words of
Ambush Commander of comp.lang.php make plain:

> It looks like in this particular case, $this->$x should be
> sufficient.

Thanks, but no.

> Otherwise, you may have to split up x into $x_object and $x_name
> (exploding by -> should be sufficient) and calling
> $x_object->$x_name. What you currently have, however, cannot be
> done.

I'm not sure what you mean.
What I'm trying to do is parse variables
out of a string and substitute them with the value. $x (in this
example) would hold the variable name extracted from the string.
I'm trying to figure out how to get to the value of that variable,
from there.

That is exactly what it does.
$x = 'string'

$$x = $this->$x;
equals:
$string = $this->string;

$x is the variable name, $variable_name holds the value......

I don't get the problem?

What is the exact reason you're trying to accomplish? Maybe if you
elaborate some more, we get the picture. Why is it this doesn't
suffice?


For example:

$this->animal = 'lamb';
$this->string = 'Mary had a little {{animal}}';

The goal is to extract 'animal' from the string (no problem), convert
it to 'lamb' and replace it in the string (again, no problem). The
question is, if I have 'animal', how do I get 'lamb'? In global
space, I would simply have 'animal' in a variable, and use $$var to
get 'lamb'.


So again, wouldn't this work:

$var = 'animal';
$this->$var; //this is translated to $this->animal, which in turn
gives you 'lamb' .


Yes, that works; I just didn't understand what AB was saying.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
May 18 '06 #9
> For example:

$this->animal = 'lamb';
$this->string = 'Mary had a little {{animal}}';

The goal is to extract 'animal' from the string (no problem), convert it
to 'lamb' and replace it in the string (again, no problem). The question
is, if I have 'animal', how do I get 'lamb'? In global space, I would
simply have 'animal' in a variable, and use $$var to get 'lamb'.

Ah. Why not use str_replace?

And when you are using classes, you could as easily define a Variable
class with a Name and a Value method, and even a ToTemplate method that
puts the two pairs of curly braces around them.

If this is for a template engine, you probably have the name/value pairs
in a "named" array. In that case, a str_replace in a foreach loop does
the job just fine.

Best regards
May 19 '06 #10
Carved in mystic runes upon the very living rock, the last words of
Dikkie Dik of comp.lang.php make plain:
For example:

$this->animal = 'lamb';
$this->string = 'Mary had a little {{animal}}';

The goal is to extract 'animal' from the string (no problem), convert
it to 'lamb' and replace it in the string (again, no problem). The
question is, if I have 'animal', how do I get 'lamb'? In global
space, I would simply have 'animal' in a variable, and use $$var to
get 'lamb'.
Ah. Why not use str_replace?


The replacing part is not a problem.
And when you are using classes, you could as easily define a Variable
class with a Name and a Value method, and even a ToTemplate method
that puts the two pairs of curly braces around them.
I'm not sure I follow you here. I'm trying to get value names *from* a
string, and then get the values for those names.
If this is for a template engine, you probably have the name/value
pairs in a "named" array.


For some values, yes. But in other cases, such as error reporting, I need
to access an actual variable.

Anyway, I have it worked out now. But thank you for answering.

--
Alan Little
Phorm PHP Form Processor
http://www.phorm.com/
May 20 '06 #11

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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.