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?