Complementing a question that I posted here for some time, about a system for registering users using SOLID standard concepts, one of the problem I came across was the question of the User class being very large and can be very variable, one Once a user can have several characteristics, it follows a situation "script":
- Each attribute has its setter / getter in the User class.
- Each attribute has its validation rule in the userValidator class.
- Each attribute has its "definition" in an associative array of the class userCrud.
Let's say that in another situation a user might have attributes like: corDOSOlhos, corDoCabelo and etc, I would have to always be changing the classes above, what would be a good solution for this?
User class structure:
class User {
private $attributes;
function __construct() {
$this->userValidator = new userValidator;
}
function setName ($param) {
if($this->userValidator->validateName($param))
$this->attributes['name'] = $param;
}
function getAttributes () {
return $this->attributes;
}
function getAttribute ($attr) {
if(isset($attributes[$attr]))
return $this->attributes[$name];
else throw new Exception("Attribute '{$attr}' does not exist");
}
}