Accessing variable variable from another class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pedrosacosta
Forum Newbie
Posts: 6
Joined: Thu Apr 22, 2010 10:50 am

Accessing variable variable from another class

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Accessing variable variable from another class

Post by requinix »

Possible, yes, but you shouldn't.

What do you need this function for?
pedrosacosta
Forum Newbie
Posts: 6
Joined: Thu Apr 22, 2010 10:50 am

Re: Accessing variable variable from another class

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Accessing variable variable from another class

Post by requinix »

Why can't you just use $_GET["foo"] and $_POST["bar"]?
pedrosacosta
Forum Newbie
Posts: 6
Joined: Thu Apr 22, 2010 10:50 am

Re: Accessing variable variable from another class

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Accessing variable variable from another class

Post 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.
pedrosacosta
Forum Newbie
Posts: 6
Joined: Thu Apr 22, 2010 10:50 am

Re: Accessing variable variable from another class

Post 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.
Last edited by pedrosacosta on Fri Apr 23, 2010 5:24 am, edited 1 time in total.
pedrosacosta
Forum Newbie
Posts: 6
Joined: Thu Apr 22, 2010 10:50 am

Re: Accessing variable variable from another class

Post 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,
Post Reply