doddsey_65 Posted November 26, 2010 Share Posted November 26, 2010 I have made a small class to learn how to do them. This basically adds the new user to the database upon registration. I would like to know if i am doing things right or if there is a better way. Also can anyone tell me why i would use this class? Previously I just included the insert query and if statements in my main register.php page and everything worked fine. what are the advantages to using it in a class? Here is the class: <?php define ("NO_USERNAME", "Please Enter A Username"); define ("NO_PASSWORD", "Please Enter A Password"); define ("NO_EMAIL", "Please Enter An Email Address"); define ("SUCCESFULL_REGISTRATION", "Done"); define ("NO_MATCH", "Your Passwords Did Not Match"); define ("UNKNOWN_ERROR", "An Unknown Error Occured"); class User { public $user_name; public $password; public $email_address; public $password_confirm; public $processed; public $error; function user_create($user_name, $password, $email_address, $password_confirm) { if (empty($user_name)) { $this->error = NO_USERNAME; } elseif (empty($password)) { $this->error = NO_PASSWORD; } elseif (empty($email_address)) { $this->error = NO_EMAIL; } elseif ($password != $password_confirm) { $this->error = NO_MATCH; } else { $this->error = NULL; } if ($this->error == NULL) { echo "Username: " . $user_name . "<br />"; mysql_query("INSERT INTO test_members (user_name, password, email_address) VALUES ('$user_name', '".md5($password)."', '$email_address')") or trigger_error("SQL", E_USER_ERROR); $this->processed = TRUE; } } public function encryptPassword($password) { $password = sha1(md5(md5(sha1(sha1($this->password))))); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/219908-class-advice/ Share on other sites More sharing options...
trq Posted November 26, 2010 Share Posted November 26, 2010 There are a bunch of things wrong with this class design wise. 1) It should not be named User if its responsibility is to create new users, it should likely be named UserManager os similar. Class names ARE important. 2) It relies on constants defined outside of the class itself. This breaks encapsulation. 3) It relies on a database connection being opened outside of the class itself. This breaks encapsulation. 4) It relies on the mysql extension. This breaks encapsulation. Quote Link to comment https://forums.phpfreaks.com/topic/219908-class-advice/#findComment-1140091 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.