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
source to share
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.
source to share
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.
source to share
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.
source to share
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();
}
source to share
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);
source to share