jonnnnnnnnnie jonnnnnnnnnie - 4 months ago 6x
PHP Question

A user registration class

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?



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.

What will you need?

  1. An object that represents a single user.
  2. An object that takes User object and saves it.
  3. 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);
        '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();

} catch (ValidationException $ve) {
   // validation failed

That's of course only an example - it can be designed totally different way.