Jump to content

Problem with constructor


chopps

Recommended Posts

 

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.