2
Supplementing a question I posted here some time ago, about a system for user registration using concepts of the SOLID standard, one of the problems I faced was the question of the User class being very large and can be very variable, since a user can have several characteristics, follows a "script" of situation:
- 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 a class associative array userCrud.
Let’s say that in another situation a user can have attributes like: colorOS, colorDoCabelo and etc, I would have to always be changing the classes above, which would be a good solution for this?
Structure of the User class:
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");
}
}
Whenever you add a new attribute you have to change the Uservalidator class. This is a violation of the O principle of SOLID. Have a validator class by attribute. Your Usercrud class I didn’t quite understand, it would be nice to post an example.
– Piovezan
I get it, thank you. on the userCRUD is simply a class that interacts with the database, takes the User object and transforms it into array and inserts, for example
– Thiago