views:

227

answers:

4

In my project I have a database class that I use to handle all the MySQL stuff. It connects to a database, runs queries, catches errors and closes the connection.

Now I need to create a members area on my site, and I was going to build a users class that would handle registration, logging in, password/username changes/resets and logging out. In this users class I need to use MySQL for obvious reasons... which is what my database class was made for.

But I'm confused as to how I would use my database class in my users class. Would I want to create a new database object for my user class and then have it close whenever a method in that class is finished? Or do I somehow make a 'global' database class that can be used throughout my entire script (if this is the case I need help with that, no idea what to do there.)

Thanks for any feedback you can give me.

A: 

Since you are using the database as an object, why not just add methods to the object that your "users class" can employ to take care of the things it needs to do. The users class can contain a pointer to the database class. The database class will protect your database, and assure that the users class is using it appropriately.

WhirlWind
That sounds a lot like mixing the two classes together. I would prefer a solution where the classes are completely independant, unless I'm missing something?
Josh
OO design is about encapsulation. To me, it makes sense to encapsulate the database independently, and then have classes that access it, so that it alone controls access to the database. However, other designs are possible. Having two classes that both access the database directly is possible, though my opinion is, that adds extra complexity.
WhirlWind
I suppose another way to do it is to instantiate a database class instance that connects to the database, then use a separate class to do "user" things via a database handle retrieved from the database class... That should work, if you prefer it.
WhirlWind
A: 

As he said, put all your functions in the database class and use the database object to access those functions from your user class. This should be the best method in your case. Eg:
global $database;
userclassvar = $database->doSomething();

Joey Ezekiel
A: 

What I like to do is make the database class with the Singleton pattern in mind. That way, if you already have a database object, it just retrieves it, otherwise creates a new one. For example:

Database.class.php

class Db
{
    protected static $_link;

    private function __construct()
    {
        // access your database here, establish link
    }

    public static function getLink()
    {
        if(self::_link === null) {
            new Db();
        }
        return self::_link;
    }

    // etc.

}


User.class.php

class User
{
    protected $_link;    // This will be the database object
    ...

    public function __construct()
    {
        $this->_link = Db::getLink();
    }

}

And now you can use User's $_link property to do the database functions, like $this->_link->query(...). You don't necessarily have to put the Db::getLink() in the constructor if your class doesn't have to interact with the database that much.

oli
+2  A: 

Simple, 3 step process. 1/ Create a database object. 2/ Give it to your user class constructor. 3/ Use it in the user methods.

Little example.

File Database.class.php :

<?php
class Database{
  public function __construct(){
    // Connects to database for example.
  }

  public function query($sqlQuery){
    // Send a query to the database
  }
  [...]
}

In User.class.php :

<?php

class User{
  private $_db;
  public function __construct(Database $db){
    $this->_db = $db;
  }

  public function deleteUser(){
    $this->_db->query('DELETE FROM Users WHERE name = "Bobby"');
  }
}

Now, in userManager.php for example :

<?php
$db = new Database();
$user = new User($db);
// Say bye to Bobby :
$user->deleteUser();

If you want the current trendy name of this old technique, google "Dependency Injection". The Singleton pattern in php will fade away soon.

Arkh
Probably the best answer I could have hoped for, I unserdtand this clearly now. Thank you. :)
Josh
... although I can't seem to accept this answer without registering, lol
Josh