Expand|Select|Wrap|Line Numbers
- class Requests {
- private $type;
- public function __construct($type='') {
- $this->type = strtolower($type);
- $this->createObject();
- }
- private function createObject() {
- switch($this->type) {
- case 'post':
- $arr = $_POST;
- break;
- case 'get':
- $arr = $_GET;
- break;
- default:
- $arr = array_merge($_POST, $_GET);
- }
- foreach ($arr as $key => $value) {
- $this->$key = $value;
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- $post = new Request('post'); //makes me use all $_POST-variables as an attribute of $post
- /*
- Say the variables stored in $_POST are the following:
- key1 => value1
- key2 => value2
- key3 => value4
- key4 => value3
- */
- echo $post->key1; // prints "value1"
- echo $post->key2; // prints "value2"
- echo $post->key3; // prints "value4"
- echo $post->key4; // prints "value3"
Expand|Select|Wrap|Line Numbers
- $var1 = $_POST['key1']; //so that $var1 = value1