PDA

View Full Version : setting scope


subhailc
11-21-2005, 09:20 AM
in js you can say
var a={prop1:value1,prop2:value2};
for(var i in a)this[i]=a[i];
alert(this.prop2)//to get back "value2";
//or
var a=document.getElementsByTagName('*');
for(var i=0;i<a.length;i++)this[a[i].id]=a[i];
//to scope ids as local object refs

how's that functionality work in php? i noticed i don't see 'this' often if at all - effectively just would like to scope the whole of REQUEST to the executing script. triedforeach($_REQUEST as $a)this[$a]=$a;
//and
foreach($_REQUEST as $a=>$b) $a=$b;but probably obviously, no good.

GJay
11-21-2005, 10:32 AM
$_REQUEST is a superglobal, that has scope everywhere.

foreach ($_REQUEST as $key=>$val) {
$$key = $val;

}

Would do what (I think) you want...

the extract() function does the same thing (takes an array and assigns the values to variables named after the keys)...

subhailc
11-21-2005, 01:28 PM
thanks for the reply gjay. i checked out extract and it looks to be just as you said, exactly what i was describing. however for whatever reasons - presuming a setting in the ini or maybe just a question of versions and coincidence, i get no returns on the server i tried where register globals is off (and thus the reason for wanting to grab REQUEST) - although it works great on the servers i tried where register globals are on - and consequently dont need it.

on the foreach - is the double $$ intentional? i tried both ways w/out luck but i can probably attribute that to the server again; before continuing i thought id ask for clarification.

thx again.

GJay
11-21-2005, 02:20 PM
The $$ is intentional, referring to a variable with the name of the value of $key.
So, if you have an array ['title'=>'Hello','price'=>'12']
$key will be 'title' on the first iteration, so the variable $title will be set to 'Hello'.
Have you tried using $_POST and/or $_GET instead of $_REQUEST? Might help narrow down the problem...