Log in

View Full Version : _POST, _GET & Classes



Acey99
11-21-2006, 05:07 PM
Is it possible to put _POST or _GET vars in a php 4+ class ??


if I had


class MyPost{

function MyPost(){
foreach ($_POST as $k => $v) $this->$k = $v;
}

}


I realize the above does not work, just wondering how to fix it.

Acey99
11-21-2006, 08:09 PM
Solved it.




class MyPost{

function MyPost(){
foreach ($_POST as $k => $v) $this->{$k} = $v;
# reminder you can also use $_GET & $_COOKIE instead of $_POST
}

}

Twey
11-21-2006, 08:38 PM
I see no real reason that wouldn't work. $_GET and $_POST are "autoglobals:" that is to say, they should be automatically defined in any function whose creation results from that request.