The correct way to get all users to use OOP

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

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

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

}

      

I'm just wondering what is the best way to get all users. create a function to get an object of this class for each user found from the database ?: s

+2


source to share


8 answers


From a pure OO point of view, User doesn't need to know databases, etc.

My first thoughts would be a UserFactory that will talk to the database and know how to create User objects from the SQL results. You can specialize in this to have SQLUserFactory, XMLUserFactory, etc.



It's worth reading about Factory Templates. Factories take care of creating objects. In this case, you may want to later distinguish between different types of user objects. In this case, the Factory's responsibility would be to decide which object to create, not the user.

+4


source


It looks like an attempt at Active Record. There are several ORM implementations in php that include Active Record. I suggest looking into this.



Otherwise, study PDO and bind query results to objects. You might be able to get something to work this way.

+2


source


Do you mean which class should be responsible for getting Users from the DB?

The custom class seems to be wrong, why an individual user "knows" about requests to get all (or some) of the users.

You can have a UserHome or UserKeeper class. and there are methods in there that return user result sets.

+2


source


Getting users from the database is not an OOP issue, it is a database issue and generally a SQL issue. Can't I understand what you mean?

0


source


The object models are somewhat artificial in this case, because applying the load () or get () method on a custom class doesn't make a lot of sense because you don't want to get 100 users with 100 separate roundtrips / picks databases. This is an example of "leaky abstractions". Implementation issues.

So, you need to write a function to run the query, return a bunch of strings, and turn them into custom objects.

Or use an abstraction like ActiveRecord (like used in CodeIgniter or Kohana) to the recordset by passing in criteria or recordset.

0


source


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, 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();

}

      

0


source


In my point of view, the interaction with the databases should be separate and put into the model by state within the zend and controlled from the controller, where you should make the model class object and call the fetch of the entire function.

This is the correct approach.

0


source


class User
{
    public $id;
    public $username;
    public $password;

    public function __construct()
    {
        $this->username = "";
        $this->password = "";

    }
}

      

separate database interactions with the interface

interface UserStore 
{
    public function Create(User $user) : User;
    //public function GetById(string $id) : ?User;
    public function getByUserName(string $userName) : ?User;
} 

      

implement the interface.

class MySqlUserStore implements UserStore
{
    public $db;

    public function __construct($db)
    {
        $this->db = $db;

    }

    public function Create( User $user) : User
    {
        $query = "INSERT INTO users (username, password) VALUES (?, ?)";
        $user->id = $this->db->insert($query, array($user->username, $user->password));
        return $user;
    }
    public function getByUserName(string $userName) : ?User
    {
        $query = "SELECT * from users where username = ?";
        $result = $this->db->select($query, array($userName));
        if(count($result) > 0) {
            $user = new User();
            $user->id = $result[0]['id'];
            $user->username = $result[0]['username'];
            $user->password = $result[0]['password'];
            return $user;
        }
        return null;

    }
} 

      

Use a custom store

 $userStore = new MySqlUserStore($this->db);     
 $user = $userStore->getByUserName($username); 

      

0


source







All Articles