2
\$\begingroup\$

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..

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

In short : its an extremely bad piece of code.

Here is a list or "whys":

  • you are mixing data access with logic ( sql queries should be handled by different object )
  • you are using global state for DB access
  • there is some mystical parameter $full in full_name() method
  • all the methods are getters
  • the SQL queries happen each time you use a getter
  • parameter $id means nothing ( is it a string , a number , some other object ? )

Basically what you have there are five standalone procedures wrapped up in an a class just for show.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the list. I improved the code by this list but im having trouble making a object that handles the sql queries can you show example on a object that you can handle the sql queries in pdo. i get stuck at the bindvalue(), how to know what to bind and not to.. \$\endgroup\$
    – Karem
    Commented Aug 10, 2011 at 1:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.