Jump to content

class advice


doddsey_65

Recommended Posts

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)))));
}

}

?>

Link to comment
Share on other sites

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.

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.