# Retrieving input from either GET/POST

Today, I created an input class with a method to check whether or not input via GET/POST exists, and another method to retrieve input from either GET/POST, without having to check both individually.

class input {
public static function exists($type = 'post') { switch ($type) {
case "post":
return (!empty($_POST)) ? true : false; break; case "get": return (!empty($_GET)) ? true : false;
break;
default:
return false;
break;
}

}

public static function get($data) { if (isset($_GET[$data])){ return$_GET[$data]; } else if (isset($_POST[$data])){ return$_POST[$data]; } else { return ''; } } }  Here is my form and example usage: <?php require_once('core/init.php'); if(input::exists()){ echo input::get('username'); echo input::get('password'); echo input::get('confirm_password'); } ?> <form method="POST" autocomplete="off"> <div class="form-group"> <label for="username"> Username </label> <input type="text" name="username" id="username" /> </div> <div class="form-group"> <label for="password"> Password </label> <input type="password" name="password" id="password" /> </div> <div class="form-group"> <label for="confirm_password"> Confirm Password </label> <input type="password" name="confirm_password" id="confirm_password" /> </div> <div class="form-group"> <input type="submit" /> </div> </form>  First of all, is it okay for me to access the methods in the way that I have? Is using statics in this way bad practice? Secondly, aside from what I asked above, are there any recommendations or improvements that you can suggest? ## 3 Answers I see a few small issues with the code: • Method names: particularly get is quite ambiguous, I'd use something more descriptive like 'get_element_data($element_name)' or something like that. As it is now it is obvious when used but it's not clear when looking just at the class itself.
• Instead of return (!empty($_GET)) ? true : false; you can actually use just return (!empty($_GET))
• You don't need to break after return. Some might, however, argue that using it anyway is a good practice that prevents errors. So choose for yourself.

Other than that, I don't see any big problems with it.

• Brilliant! Thanks for your help :). Regarding your third point, I think I'll carry on using the break just to build up the habit so that I don't forget when it actually is needed. – user21611060 Dec 11 '14 at 23:30

has a native GLOBAL variable $_REQUEST that you can check to see if $_GET or $_POST was assigned. By the same one you can retrieve the values from that: <?php$name = isset($_REQUEST['name'])?: false; //this will check whether the sent data was GET or POST ?>  Also, you do not need to create a class to solve this problem. This one (there are several that do the same) https://github.com/auraphp/Aura.Http can be a good solution for you. I recommend that you read about HTTP RFC. • But doesn't the$_REQUEST take the $_COOKIE values too? – user21611060 Dec 15 '14 at 17:43 • No no. I guess you are new to OOP and you are getting out of the way like me on past =) . First, avoid static methods always you can. They are inside OOP, but are not good for maintainability, testability and another good things.... Cookie are just cookies, Request are just Requests. Everyone in the right place, ok? It is not good to "mix" things in OOP, search for "Single Responsibility Principle" and you will understand what I mean. – Daniel Lima Dec 15 '14 at 17:50 Just a note: In your input class you made a function. exists($type = 'post') that make difference between get and post methods. But you made just a get($name) function that use $_GET and $_POST arrays. I think you should make different functions for get and post methods or just add a secound parameter ($type like exists() function) to your get function to be clear everyone what your function does.

When you use your function(s) you have to know which method do you use because the exists() function need it as parameter.