Page 1 of 1

Accessing variable variable from another class

Posted: Thu Apr 22, 2010 10:55 am
by pedrosacosta
Hi,

I've the following code in getpost.php:

Code: Select all

function getParams($h) {
  if (! empty ( $h )) {    
   foreach ( $h as $key => $value ) {
     $$key = $value;
   }
  }
}
Notice that the code isn't inside a class. It's only a function.

Now, I would like to access the created variables from another PHP file. Is it possible when the function isn't inside a class?

Thanks,
PSC

Re: Accessing variable variable from another class

Posted: Thu Apr 22, 2010 1:23 pm
by requinix
Possible, yes, but you shouldn't.

What do you need this function for?

Re: Accessing variable variable from another class

Posted: Thu Apr 22, 2010 1:57 pm
by pedrosacosta
This function is to get all the variables that is passed by $_GET or $_POST. I've put this function in another php file, because this function will be accessed by several php files.

Re: Accessing variable variable from another class

Posted: Thu Apr 22, 2010 3:13 pm
by requinix
Why can't you just use $_GET["foo"] and $_POST["bar"]?

Re: Accessing variable variable from another class

Posted: Fri Apr 23, 2010 4:07 am
by pedrosacosta
I don't use $_GET["foo"] or $_POST["bar"], because independently of the context that I'm using the site, I can have several variables with several names. I just created that function to use in all the possible contexts.

I don't want to offend anyone, but I just asked how can I do it, and I'm not questioning how should I design a site.

Thanks,
PSC

Re: Accessing variable variable from another class

Posted: Fri Apr 23, 2010 4:53 am
by requinix
pedrosacosta wrote:and I'm not questioning how should I design a site.
That's right, you aren't. But I am. Meh, whatever.

PHP already has a function for this: extract. It even puts the variables into the right scope, which is the solution that your custom function can't deliver.

Re: Accessing variable variable from another class

Posted: Fri Apr 23, 2010 5:09 am
by pedrosacosta
This function has the purpose to retrieve all variables that are passed in GET or POST in several contexts. This function is to be applied in several PHP files. Instead of copying this function to several PHP files, I just want that these PHP files will call this function.

That is the reason. I can't find any better way to design this. I just don't want to replicate this function.

For example, A.php, B.php, ..., Z.php files will need to retrieve the GET and POST variables. I just want a single place where this function will be to retrieve these variables to all the files.

And in A.php I've the line:
echo $var1;

This $var1 is a variable that is passed by GET or POST.


I don't put $var1=$_GET["var1"], because PHP offers a better way to create and retrieve variables in few lines.

Sorry, I don't want to offend anyone.

Re: Accessing variable variable from another class

Posted: Fri Apr 23, 2010 5:18 am
by pedrosacosta
I'm newbie in PHP, but I think that your solution isn't what I want. I just want to access variables that are defined inside a function in another PHP file. Can you give me an example how can I do what I want using the extract method?

Thanks,