chopps Posted May 30, 2011 Share Posted May 30, 2011 Hello All, I have been having a problem recently with a constructor function that is responsible for encrypting the password every time a user logs in or creates a new account. I am using PHP OOP and the constructor function belongs to the user class. I have included the snippet below: ********************* function __construct($encrypt_pass) { $this->password = sha1($encrypt_pass); } ********************* I have the following code in the head of my register form which calls the create function once everything has been verified: <?php if ((isset($_POST['register'])) && ($_POST['email']) == ($_POST['email_verify'])) { $username = trim($_POST['username']); $password = trim($_POST['password']); $email = trim($_POST['email']); $first_name = trim($_POST['first_name']); $last_name = trim($_POST['last_name']); $create_new_user = new User($password); $create_new_user->username; $create_new_user->password; $create_new_user->email; $create_new_user->first_name; $create_new_user->last_name; $create_new_user->create(); $message = "Please check you inbox and follow the instructions to verify your account; Otherwise it will be deleted after seven days."; } elseif ((isset($_POST['register'])) && ($_POST['email']) != ($_POST['email_verify'])) { $message = "The email addresses did not match! Please enter them again."; } else { $username = ""; $password = ""; $email = ""; $email_verify = ""; $first_name = ""; $last_name = ""; } ?> ****************************** I'm not sure if I'm using the constructor correctly or not. When I instantiate the object it will give me an error unless I pass in the $password variable, $create_new_user = new User($password);, but every time I do it fails to input the other fields into the database. I know the create function is working correctly because I commented out the constructor and removed the $password variable from the initial object call and everything worked fine. Please let me know what I am doing wrong. Any help would be very much appreciated. Link to comment https://forums.phpfreaks.com/topic/237865-problem-with-constructor/ Share on other sites More sharing options...
trq Posted May 30, 2011 Share Posted May 30, 2011 Your __construct() and call to instantiate the object looks fine, you are however not setting any of the other properties. eh; This..... $create_new_user->username; $create_new_user->password; $create_new_user->email; $create_new_user->first_name; $create_new_user->last_name; does nothing. Link to comment https://forums.phpfreaks.com/topic/237865-problem-with-constructor/#findComment-1222322 Share on other sites More sharing options...
chopps Posted May 30, 2011 Author Share Posted May 30, 2011 Oh, lol! Good catch. Strange that it worked when I commented out the constructor though. I set the properties equal to the variables declared earlier and it worked perfectly. Thanks for the quick reply. Link to comment https://forums.phpfreaks.com/topic/237865-problem-with-constructor/#findComment-1222336 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.