I assume that the register() function you have is responsible for inserting a user's details into a database? And it might look something like:
PHP Code:
class User {
register($username, $email, $dob, $icq, ...);
}
The approach I would take, the approach that makes most sense to me would be to move register() into a seperate class who's sole responsibility would be to retrive, insert or query users.
Use the user class just to store user properties. Instantiate a user and then populate it's properties either directly or via "setter" methods.
You would then pass this user object to your new class that would be responsible for inserting this new user into the database.
PHP Code:
class User_Store {
public function store(User $user) {
// ...
}
}
// just for illustration
$newUser = new User();
$newUser->setName($_POST['name']);
$newUser->setEmail($_POST['email']);
$userStore = User_Store::getInstance();
$userStore->insert($newUser);
If you're wondering why you should do this, then think about what would happen if you decided to change the registration details that you gather from new registrants - i.e. if you change what user details you store. You would have to change the method signature of your register() function.
This may not seem like a big deal but when you start dealing with optional parameters things start to get messy. Forms often contain data that is optional.
The worse case scenario is that you add an optional parameter, and then decide to add another parameter which is not optional which will alter the order of the parameters, requiring all code to adapt to these changes.
You would also need to modify the body of the register() function.
Using my approach (in PHP5) you would only need to modify the body of the User_Store::store() method.
However, what I forgot to mention in my previous post is that a lot of this comes down to how you feel about functions with lots of parameters.
HTH
Bookmarks