tags:

views:

134

answers:

8

Hello,

ive got a user class where i can get the name, date of birth etc of the user... (below)

class user {
    private $id;
    private $name;
    private $dob;
    private address;

    function __construct($id) {
         $this->id = $id
    }

}

i was just wondering what the best practice to get all users. do i create a function to get an object of this class for each user found from the database? :s

A: 

Getting users from a database isn't an OOP question, it's a database question, and typically an SQL question. Unless I don't get what you mean?

blwy10
yeah i know its SQL, i know how to do that, i was just wondering how to do it, should i create a getAll method in the user class? im not sure
Matt
You should have a separate function, or class, or something that runs the SQL query, and create these objects. The user object should be separate from the SQL functionality.
blwy10
I think this *is* an OOP question, in that he's asking where the responsibility lies, not how to do it.
Brian Agnew
A: 

Object models in this case are somewhat artificial because putting a load() or get() method on the user class doesn't make a lot of sense because you don't want to get 100 users with 100 separate database roundtrips/selects. It's an example of "leaky abstractions". Implementation matters.

So you're going to need to write a function to run a query, return a bunch of rows and turn them into user objects.

Or use an ActiveRecord-like abstraction (eg that used in CodeIgniter or Kohana) to a set of records by passing in a criteria or a set of records.

cletus
+2  A: 

This looks like an attempt at Active Record. In php there are a few ORM implementations that include Active Record. I suggest looking into that.

Otherwise look into at PDO and binding query results to objects. You might be able to get something working that way.

Phillip Jacobs
The way I see the question, OP already is/could be using a design like Active Record for each row. This more about handling "multiple active records".
chelmertz
+2  A: 

Do you mean which class should have the responsibility for getting Users from the DB?

The User class seems wrong, why would an indivudual user "know" about queries to obtain all (or some) users.

You could have a UserHome or UserKeeper class. and in there have methods which return result sets of user.

djna
I would take this advice and look at the Factory and Abstract Factory design patterns, depending on your class hierarchy.
Thomas Owens
A: 

Assuming your users are stored in a MySQL database, I would create the following static method in Users. Well, I would probably use an ORM, like Doctrine instead, but...

static function queryAll() {
  $query = "SELECT id,name,dob,address FROM user";

  if ($result = $mysqli->query($query)) {
     $all = array();
     while ($obj = $result->fetch_object()) {
       $all[$obj->id] = $obj;
     }
     $result->close();
     return $all;
  }

  return array();

}
Zed
+4  A: 

From a purely OO perspective, the User shouldn't have knowledge of databases etc.

My first thoughts would be a UserFactory, which will talk to the database and know how to build User objects from SQL results. You may want to specialise this to have a SQLUserFactory, an XMLUserFactory etc.

It's worth reading about Factory patterns. Factories look after the creation of objects. In this scenario, you may want to later distinguish between different types of User objects. In this scenario it would be the Factory's responsibility to decide what sort of object to create, not the User's.

Brian Agnew
A: 

Getting all users connected with OOP would mean to get a new instance of a table class whereas getting only one user would be getting a new instance of a row class.

This of course means that you have written an abstract database (or other data source) mapper class (ORM) and have extended this class for your user table.

So if you have a table class users you would instantiate it for example like this:

$users = new Users()

Now $users is a database table object and you can do with your users whatever you like.

tharkun
A: 

In my point of view Database interaction should be separate and put in model as of in zend framework and controlled from controller where you should make of object of model class and call fetch all function.

Is this is the right approach.

neverSayNo