patternphpMinor
Retrieving input from either GET/POST
Viewed 0 times
inputeitherpostgetretrievingfrom
Problem
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.
Here is my form and example usage:
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?
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:
Username
Password
Confirm Password
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?
Solution
I see a few small issues with the code:
Other than that, I don't see any big problems with it.
- Method names: particularly
getis 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 justreturn (!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.
Context
StackExchange Code Review Q#73398, answer score: 4
Revisions (0)
No revisions yet.