用户注册类 [英] A user registration class
问题描述
我在PHP中写这个,但是,一般来说,用户(注册?)类有什么方法和属性?我知道我可以使用PEB中的MDB2进行数据库抽象,我可以想到的一个特性是确保用户名是有效的。
I'm writing this in PHP, but, generally speaking, what methods and properties would a user (registration?) 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?
感谢。
编辑:
用户名验证自己的权利(Validator类或某事)?
Username validation would be a class of its own right (Validator class or something)?
推荐答案
不应该有这样的类。注册是接收数据,验证数据,最后,如果验证成功,处理验证的数据的整个过程。换句话说:注册是使用一些对象来解决给定问题的代码部分。
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.
你需要什么?
- 代表单个用户的对象。
-
User
对象并保存它。 - 一些验证机制(至少会使用几个对象)。
- 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
}
$ b b
这当然只是一个例子 - 它可以完全不同的方式设计。
That's of course only an example - it can be designed totally different way.
这篇关于用户注册类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!