I have a users model that has a lot of properties such as first_name, last_name, email, address, address2…etc
I am writing a php class to manage these properties, but it seems like I am writing a lot of the same code. (Getters and setters). Should I use magic methods to manage this? It seems like an OK idea, but I don’t want incorrect properties being set. Any ideas?
<?php
class User
{
private $username;
private $email
private $first_name;
private $last_name;
private $address;
private $address2;
function __construct()
{
}
function getUsername()
{
return $this->username
}
function setUsername($username)
{
$this->username = $username;
}
...
}
?>
Unless you are doing validation on the input, there’s no point using getters/setters here in my opinion.
Make the properties
public, and override the getters/setters for invalid properties using magic methods: