I'm writing this in PHP, but, generally speaking, what methods and properties would a user (regi开发者_运维问答stration?) class have? I know I can use MDB2 from PEAR for database abstraction, and one feature I can think of is making sure the username is valid.
$user->validateUserName($username, $regex);
Can anybody think of any more?
Thanks.
Edit:
Username validation would be a class of its own right (Validator class or something)?
Well, you should check if
- Username is already in use
- Password is strong enough
- User's Mail address is valid
You also need methods to
- create a new account
- change User's data
- delete an existing account?
- allow Users to recover forgotten pws
There shouldn't be a such class at all. Registration is a whole process of receiving data, validating that data and finally, processing validated data if validation succeed. In other words: registration is that part of code which uses some object to solve a given problem.
What will you need?
- An object that represents a single user.
- An object that takes
User
object and saves it. - Some validation mechanism (it will use at least several objects).
Let's design an interface. How will we use the code we're going to write?
try {
$validator = new Validator($_POST);
$validator->addRules(array(
'username' => new LengthValidator(array('min' => 3, 'max' => 15)),
'password' => array(
new LengthValidator(array('min' => 6))
new RegexpValidator(array('pattern' => '#...#'))
),
'email' => array(
new EmailValidator(),
new UniqueValidator(....)
)
));
$data = $validator->validate();
$userService = new UserService($databaseHandler, $someOtherArguments);
$user = new User();
$user->setUsername($data['username']);
$user->setPassword($data['password']);
$user->setEmail($data['email']);
$user->setFirstname($data['firstname']);
$user->setLastname($data['lastname']);
$userService->save($user);
} catch (ValidationException $ve) {
// validation failed
}
That's of course only an example - it can be designed totally different way.
ValidForm, it's a great class structured to handle your forms both client/server-sided. However, no MySQL is passed through the form.
精彩评论