I need some constructive feedback for the working OOPs project which I created to introduce to Object Oriented Programming in a class. Hence is for beginners, I did not include interfaces or abstract functions nor singleton or factories. Can you give me some feedback on the code style, some possible anti patterns? Thanks
<?php
declare(strict_types=1);
namespace MidoriKocak;
use function array_key_exists;
/**
*
*/
final class User
{
/**
* @var Database
*/
private Database $db;
/**
* @var string
*/
private ?string $id = null;
/**
* @var string
*/
private string $email;
/**
* @var string
*/
private string $username;
/**
* @var string
*/
private string $password;
/**
*
*/
public function __construct(Database $db)
{
$this->db = $db;
}
/**
* @param string $password
*
* @return void
*/
public function setPassword(string $password): void
{
$this->password = password_hash($password, PASSWORD_DEFAULT);
}
public function getPassword(): string
{
return $this->password;
}
/**
* @param string $email
*
* @return void
*/
public function setEmail(string $email): void
{
$this->email = $email;
}
/**
* @param string $username
*
* @return void
*/
public function setUsername(string $username): void
{
$this->username = $username;
}
/**
* @return void
*/
public function save()
{
if ($this->id) {
$this->db->update($this->id, $this->toArray(), 'users');
} else {
$this->db->insert($this->toArray(), 'users');
$this->id = $this->db->lastInsertId();
}
}
/**
* @return array
*/
public function toArray(): array
{
return [
'username' => $this->username,
'email' => $this->email,
'password' => $this->password
];
}
/**
* @param array $data
*
* @return void
*/
public function fromArray(array $data): void
{
if (array_key_exists('id', $data)) {
$this->id = (string)$data['id'];
}
$this->setEmail($data['email']);
$this->setUsername($data['username']);
$this->setPassword($data['password']);
}
}
Thank you for your feedback.
(P.S. Please be harsh as possible.)