Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'd do it something like this (using another class):</p> <pre><code>class UserRepository { public function getByName($name) { $result = mysql_query("SELECT userId, email, user_group FROM user WHERE name = '$name'"); $users = []; while ($row = mysql_fetch_assoc($result)) { $user = new User; $user-&gt;email = $row['email']; $user-&gt;userId = $row['userId']; $user-&gt;userGroup = $row['user_group']; $users[] = $user; } return $users; } } </code></pre> <p><strong>Addition:</strong> The following example gives a good idea on how you can make the classes more testable and easy to modify in the future should they need to be:</p> <p><strong>UserRepositoryInterface</strong> </p> <pre><code>interface UserRepositoryInterface { public function getByName($name); public function getByUserId($id); } </code></pre> <p><strong>MySqliUserRepository</strong> </p> <pre><code>class MySqliUserRepository implements UserRepositoryInterface { public function getByName($name) { // Get by name using mysqli methods here } public function getByUserId($id) { // Get by user id using mysqli methods here } } </code></pre> <p><strong>PDOUserRepository</strong> </p> <pre><code>class PDOUserRepository implements UserRepositoryInterface { public function getByName($name) { // Get by name using PDO methods here } public function getByUserId($id) { // Get by user id using PDO methods here } } </code></pre> <p><strong>Usage</strong></p> <pre><code>class Foo { protected $userRepository; public function __construct(UserRepositoryInterface $userRepository) { $this-&gt;userRepository = $userRepository; } public function bar() { $user = $this-&gt;userRepository-&gt;getByUserId(10); } } </code></pre> <p><strong>Regarding use of <code>mysql_</code></strong></p> <p>It may not be exactly how you do it but it'll give you an idea. Also <code>mysql_</code> is depreciated so its best to use <code>mysqli_</code> or <code>PDO</code> (my personal recommendation). <code>PDO</code> is also much more OOP friendly.</p> <p><code>PDO</code>: <a href="http://php.net/manual/en/book.pdo.php" rel="noreferrer">http://php.net/manual/en/book.pdo.php</a></p> <p><code>mysqli_</code>: <a href="http://php.net/manual/en/book.mysqli.php" rel="noreferrer">http://php.net/manual/en/book.mysqli.php</a></p> <p><strong>Update:</strong></p> <p>Your individual user class would simply contain information relating to the user. The user class shouldn't contain any way to retrieve a user, that is the job of the repository. So if you want to retrieve 1 user, instead of doing in the <code>User</code> <code>__construct</code> as you currently do, add a method to the <code>UserRepository</code> that looks something like this:</p> <pre><code>public function getByUserId($id) { // Select user from db, check only 1 exists, make user object, return. } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload