There is a users
table in the database, the task: to make a model (MVC) with the ability to select certain table fields.
users.php class Users { private $id = null; private $login = null; private $password = null; public function __construct($login, $password) { $this->login = $login; $this->password = $password; } // All Setters && Getters for all cells of db // ag public function SetLogin; public function GetLogin static public function getInstance($id) { $row = "SELECT * FROM `users` WHERE `id` = '{$id}' "; if($row){ $obj = new Users($row['login'], $row['password']); $obj->setId($row['id']); return $obj; } } }
By convention, the users
table has many cells, and it is not always necessary to select a record with all the cells, in other words, a request to getInstance
needs to be done with certain cells, for example (SELECT login FROM users)
; How, then, to make a call to an object, because the constructor needs all the cells as arguments?
Alternatively, you can pass an array to the constructor, and in the constructor for each property, check the value in the array.
$this->password = isset($array['password']) ? $array['password'] : null;
But is there a more concise option?