Closed Thread Icon

Preserved Topic: php eval() question (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=21171" title="Pages that link to Preserved Topic: php eval() question (Page 1 of 1)" rel="nofollow" >Preserved Topic: php eval() question <span class="small">(Page 1 of 1)</span>\

 
RypTide
Nervous Wreck (II) Inmate

From: Manassas VA, USA
Insane since: May 2002

posted posted 06-12-2002 15:42

I'm trying to use eval within a class to set a global variable. I can't get the syntax just right.

basically I'm looping through the attributes of an xml tag, and trying to set each attribute as a variable.

here is some of the code:

foreach($attrs as $key => $val) {
$this->html .= $key."=".$val."<br>";
eval ("\$$key = \"$val\";");
}

now this eval will set the variable just fine the way I have it, I've tested that, but the variable can't be used out of the class/function. How do I go about making that so? Am I even on the right track here in terms of global variables?


RypTide

"Music is the vernacular of the human soul" ~ Geoffrey Latham

dk01
Bipolar (III) Inmate

From: dk's house of love
Insane since: Oct 2001

posted posted 06-12-2002 16:43

Ok I am not an expert but can't you declare the varible outside the function and then you are able to use it globally. This is the way its done in javascript at least. Here's how i'd do it in js:
[code]
<script>
var myGlobal;

function whatever(myVal) {
myGlobal = myVal;
}
</script>

.......
whatever(2);
[code]

This should set myGlobal equal to 2. I am not sure but I think php does it the same way. Correct me if I'm wrong.
-dk

- can't decide? have another drink.

RypTide
Nervous Wreck (II) Inmate

From: Manassas VA, USA
Insane since: May 2002

posted posted 06-12-2002 17:18

That would work for setting up a global var, so what I think I need now is to set it up as an array, then in my loop, just set up everything in the array.

would my function then need to return the array ?

I'll let you know how it goes.

RypTide

"Music is the vernacular of the human soul" ~ Geoffrey Latham

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 06-12-2002 17:54

dk, you can't do that with PHP as you can in JS (actually JS is one of the few languages that let's you do that) With PHP you need to use "global varname" within your function to bring in a variable set outside the scope of the function.

to grab functions outside of classes you need to either return them or you need to refrence them into the class.
You need to use the "this" keyword to scope variables within a class.

Here's a simple example

class do_nothing {
var $MyVariable = 1;

function addOne() {
$this->MyVariable++;
return $this->MyVariable;
}

}

Ok now to use this what you first need to do is instantiate the class;

$doNada = new do_nothing();

Then call the function you can either do
$doNada->addOne();

or

$myVariable = $doNada->addOne();


if you do just the first then you can refrence the variable like so
$doNada->MyVariable which is now equal to 2. (this is how you refrence variables within the class)

or with the second you can just use $myVariable since you returned "2" to it.

Note these are not the same variables The trick is that if you change $doNada->MyVariable, say:
$doNada->MyVariable = 3;

And then call $doNada->addOne(); again

Then the variable is now 4. If you change $MyVariable then the addOne will still be at 3.


hmm.. this may confuse more than it helps.



.:[ The Tao of Steve ]:.
Be Desireless
Be Excellent
Be Gone
...................................

RypTide
Nervous Wreck (II) Inmate

From: Manassas VA, USA
Insane since: May 2002

posted posted 06-12-2002 18:01

I actually just figured it out...

here is what I did in summary.

This is part of a class that I am working on.

var $a_array = array();

function startElement($parser, $name, $attrs) {
foreach($attrs as $key => $val) {
$this->a_array["$key"] = "$val";
}

this adds an element to an array (a_array) of all off the attributesof the xml tags.


then when I want an item from the array I can do $class->a_array["foo"]


for the next version of this class, I will probably do a multidimensional array, so every tag will have a seperate array underneath the attr array.

so i can use

$bigarray["USERINFO"]["NAME"] etc


thanks all for your input

RypTide

"Music is the vernacular of the human soul" ~ Geoffrey Latham

« BackwardsOnwards »

Show Forum Drop Down Menu