i have some settings stored in a mysql database table "settings" for a php web application. The table has two columns, simply "option_name" and "option_value". I've build a class to retrieve or change those settings (usually are changed through checkboxes) and i was wondering if it could be considered a good approach:
class Settings {
public function __construct(PDO $db_connection) {
$this->db_connection = $db_connection;
}
public function getValue($setting_name) {
$sql = "SELECT option_value FROM settings WHERE option_name = ?";
$stmt = $this->db_connection->prepare($sql);
$stmt->execute([$setting_name]);
return $stmt->fetchColumn();
}
public function setValue($setting_name, $value) {
// Some code here to sanitize value
if (!is_bool($value)) {
throw new InvalidArgumentException("value must be boolean");
}
$sql = "UPDATE settings SET option_value = ? WHERE option_name = ?";
$stmt = $this->db_connection->prepare($sql);
return $stmt->execute([$value, $option_name)];
}
}
This way every time a method is used, a query is sent to database. Also the class is just a 'wrapper' for the queries. Is this considered a good way to manage settings? I'd love to hear some suggestions