Problems with php Classes

Hi! I have been working on getting a php class to work but it just wont seem to work.

The function inside the class works as a standalone function, however, when incorporated into a class it stops working.

if anyone could help it would be amazing!
[php]
class Registration {

public function __construct()
{
	session_start();
	$this->registerNewUser($dbh,$_POST['pass'], $_POST['fname'], $_POST['lname'], $_POST['email'], $_POST['sex-select'], $_POST['day'], $_POST['month'], $_POST['year']);
}

private function registerNewUser($dbh,$password,$first_name,$last_name,$email,$gender,$day,$month,$year)
{

$options = [
‘cost’ => 12,
‘salt’ => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),
];

$salt = $options[‘salt’];
$passwordHash = password_hash($password, PASSWORD_BCRYPT, $options);
$dob = new DateTime($year.’-’.$month.’-’.$day);

			$stmt = $dbh->prepare('SELECT * FROM limbo WHERE email=? LIMIT 1');
			$stmt->bindParam(1, $email, PDO::PARAM_INT);
			$stmt->execute();

			$checkEmail = $stmt->fetch(PDO::FETCH_ASSOC);

	      		if($checkEmail) {

		     		header("location: index.php");
		     		exit;

            	} else {

            		$stmt = $dbh->prepare("INSERT INTO limbo ( first_name, last_name, email, password, salt, gender, dob) VALUES (:first_name, :last_name, :email, :password, :salt, :gender, :dob)");
	                $stmt->bindParam(':first_name',	 $first_name);
	                $stmt->bindParam(':last_name',	 $last_name);
    	                $stmt->bindParam(':email',		 $email);
        	        $stmt->bindParam(':password',	 $passwordHash);
            	        $stmt->bindParam(':salt',		 $salt);
      	    	        $stmt->bindParam(':gender',		 $gender);
        	    	$stmt->bindParam(':dob',		 $dob->format('Y-m-d'));
        	   	$stmt->execute();

               $passwordV = password_verify($password, $passwordHash);

               if ($passwordV){
                    $stmt = $dbh->prepare('SELECT * FROM limbo WHERE email=? LIMIT 1');
                    $stmt->bindParam(1, $email, PDO::PARAM_INT);
                    $stmt->execute();
                    $result = $stmt->fetch(PDO::FETCH_ASSOC);

                    	if($result)	{
                    		$_SESSION ["user_id"] = $result ["user_id"];
                    		$_SESSION ["email"]   = $result ["email"];
                    		$_SESSION ["fname"]   = $result ["first_name"];
                   			header("location: profile_builder.php");
                		}
                }else{
                echo "Error: Cannot register.";
               }
            }
        }
    }

[/php]

i call the class file with $register = new Registration();

i cant see what im doing wrong. maybe it is lack of understanding?

Well for starters $this is usually assign to an argument (variable) and in your case it isn’t.

[php]$this->registerNewUser($dbh,$_POST[‘pass’], $_POST[‘fname’], $_POST[‘lname’], $_POST[‘email’], $_POST[‘sex-select’], $_POST[‘day’], $_POST[‘month’], $_POST[‘year’]);[/php]

I have to fix dinner, but I’ll look some more in-depth at your code after dinner. However, I make this suggestion either pickup a good book on PHP OOP (I recommend Larry Ullman) or a updated tutorial on PHP OOP.

thanks for the speedy reply! however i have seen $this->methodName(); used in other classes. Is there a special rule in which it has to follow?

[php]class MyClass {

public $foo; // Needs to be define as an argument (variable); 

public function __construct() {
  $this->foo = "This is what I meant";

}
}[/php]

as for $this->methodName(); that is calling an already defined Method (Function what people normally think it as).

While I’m at it do this for the password hashing portion of the script
[php]$options = [ ‘cost’ => 12, ‘salt’ => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM) ];
$passwordHash = password_hash($password, PASSWORD_BCRYPT, $options);[/php]

Instead of what you have, for the password_hash stores the “salt” as is with the password, that is why we don’t create our own. :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service