开发者

OOP user system logic

i've been searching for an approach to an object oriented user system but I couldn't find any tutorial that answers my questions. I really don't care about the code stuff, I can do that later, but the logic of the OOP is really killing me (oh, I'm new to OOP, I forgot to mention it).

If I have a mysql db with users, and a php file with two classes, a dbmanager and a user class, is ok to use dbmanager'开发者_JAVA百科s methods inside the user class?

If not, how should I do to make the user class and the db class interact with each other?

Thanks

---EDIT---

Would this approach be right? What's the benefits of using, for example, extended classes?

    class db{
    function checkUser($login, $password){ 
        $password = sha1(md5($password)); //encrypt password
        $query = "SELECT * FROM users WHERE (login='$login' OR email='$login')";
        $result = mysql_query($query);

        if ($user = mysql_fetch_assoc($result)) {
            if ($user["password"] == $password) {
                //there's a user class somwhere else
                $checkeduser = new user($user);
                return $checkeduser;
            } else return true; //bad password
        } else return false; //user not registered
    }

    $user = $dbconnection->checkUser($login, $password); //encrypted password
    if(is_object($user)) { //if logs in
    $_SESSION["user"] = serialize($user);
        header("Location:index.php"); //go to home
    }


One of the models is that you would have generic class which translates between DB and objects, e.g. by reading a row from the DB and assigning it to properties of the object, like this:

 abstract class DBTable {
      function __construct(DBDriver $db) {
         $this->db = $db;
      }
      function loadData($id) {
          $row = $this->db->query("SELECT * FROM {$this->table_name} WHERE id=?", $id);
          foreach($row as $key => $value) {
              $this->$key = $value; 
          }
      }
  }

  class User extends DBTable {
        public $table_name = "users";
  }

  $db = new MySQLDBDriver();
  $user = new User($db);
  $user->load("joe");

The real DB handling is in DBManager, the relation between objects and DB in DBTable. You may look at Zend Framework as an example of such model: http://framework.zend.com/manual/en/zend.db.table.html


There is absolutely nothing wrong with using instances of one class inside another class. It is referred to as composition.

Another approach is inheritance where subclasses inherit methods and properties from parent classes and expand on parent class functionality.

What this means in practice:

  • If you have a user class that has a database class instance as one of its members/properties, you can call all public methods of the database class on the member that contains an instance of the class.

  • If you have a user class that extends a database class, you should be able to use (some of) the parent (database) class methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜