I am still uncomfortable and new with working with classes.
I have made a User class that will return stuff about the user.
<?
class User {
public function age($id) {
$birth = new DateTime("$id");
$now = new DateTime();
$age = $now->diff($birth)->format("%y");
return $age;
}
public function avatar($id) {
$connect = Database::factory();
$id = $id;
$getQuery = $connect->prepare("SELECT photo_thumb FROM users_profile WHERE uID =:id");
$getQuery->bindValue(":id", $id);
$getQuery->execute();
$show = $getQuery->fetch();
$photoName = ((empty($show['photo_thumb'])) ? '' : $show['photo_thumb']);
return $photoName;
}
public function fullname($id, $full = 1) {
$connect = Database::factory();
$query = $connect->prepare("SELECT firstname, lastname FROM users WHERE id =:id");
$query->bindValue(":id", $id);
$query->execute();
$row = $query->fetch();
$full_name = $row["firstname"]." ".$row["lastname"];
return $full_name;
}
public function firstname($id) {
$connect = Database::factory();
$query = $connect->prepare("SELECT firstname, lastname FROM users WHERE id = '$id'");
$query->execute();
$row = $query->fetch();
$firstname = $row["firstname"];
return $firstname;
}
public function sex($id) {
$connect = Database::factory();
$query = $connect->prepare("SELECT sex FROM users WHERE id=:id");
$query->bindValue(":id", $id);
$query->execute();
$row = $query->fetch();
return $row["sex"];
}
}
If you look through it fast, you will notice how everything is like the same, apart from its grabbing another column name and sometimes from another table.
Otherwise it just execute, fetches and returns.
I wonder if I can improve this code any way, so it looks cleaner and not so copy+paste like now..