So I've just started coding in classes and I haven't quite got my head around it yet. So I understand the concept of OOP but I have a question.
I have "users" stored in a database, and I want a user class that will get information I might want about a user and do things related to a "user" object. I understand that.
So say I have a user class, with properties username and email I have the following code
<?php
class User {
private $username;
private $email;
public function __construct($username,$email) {
$this->username=$username;
$this->email=$email;
}
}
?>
Now I know I should have getters and setters to access username and email. Getters, I understand. But setters - do I need these? And if I do, should these update that field in the database for that user? Would my setter for email update the database as well as the $this->email ? It may seem like a silly question but I want to get this right.
Then finally what is the best way to get users from the database and create objects? Say elsewhere I want to create a user object from ID 50 - which would get user with ID 50 from the database and construct a user object with the database email and username fields, what is the best way to do this?
Thanks!