Jump to content

PHP Class - How to Turn a "Normal" Script Into Classes!


ThatMatt

Recommended Posts

Hey!

 

I've been trying to understand Classes for a long time. But can't really get it. So  how can you turn a code like this into a class.

 

<?

include "config.php";

 

if(isset($_GET['register'])) {

 

if((!$_POST[username]) || (!$_POST[password]) || (!$_POST[cpassword]) || (!$_POST)) {

 

echo "Sorry! You left a field blank!";

 

}else{

 

$username = $_POST[username]; 

$password = $_POST[password]; 

$cpassword = $_POST[cpassword]; 

$email = $_POST;

 

if($password == $cpassword)

{

 

$password = sha1(md5(md5(sha1(md5(sha1(sha1(md5($password)))))))); 

$cname = mysql_query("SELECT `username` FROM `users` WHERE `username` = '$username'"); 

$cname= mysql_num_rows($cname); 

 

if($cname>=1) { 

echo "The username is already in use"; 

}else{

 

$username = addslashes(htmlspecialchars($username)); 

$email = addslashes(htmlspecialchars($email));

 

$adduser = mysql_query("INSERT INTO `users` (`username`, `password`, `email`) VALUES('$username','$password','$email')");

 

mail("$email", "$config[name]", "Dear $username

 

Thank you for registering on $config[name]. We hope to hear

from you soon!

 

Thanks!

$config[name] Staff!", "From: no-reply@$config[name]");

 

$adduser = mysql_query("INSERT INTO `users` (`username`, `password`, `email`, `userlevel`) VALUES('$username','$password','$email','2')");

echo "You are now registered,<br><br>You can now login to your account";

}

 

}else{

echo "Your password and conformation password do not match!";

}

}

}else{

 

echo "<form action='register.php?register' method='post'>

<table width='350'>

  <tr>

    <td width='150'>Username:</td>

    <td width='200'><input type='text' name='username' size='30' maxlength='25'></td>

  </tr>

  <tr>

    <td>Password:</td>

    <td><input type='password' name='password' size='30' maxlength='25'></td>

  </tr>

  <tr>

    <td>Confirm Password:</td>

    <td><input type='password' name='cpassword' size='30' maxlength='25'></td>

  </tr>

  <tr>

    <td>Email:</td>

    <td><input type='text' name='email' size='30' maxlength='55'></td>

  </tr>

  <tr>

    <td colspan='2'><center><input type='submit' value='Register'></center></td>

  </tr>

</table>

</form>";

}

?>

Looking at that code there is no real point turning it into a class, you wouldn't gain any benefit.

 

the main idea of classes are they are a persistent object making it easier to have multiple objects of the same type, you use classes if you need more than one object of the same type.

 

eg a form generator, you may have multiple forms on a page if you want to keep track of all the elements, the form name, the action ect for 8 forms it would be very hard to keep track of all those variables thats where classes come in you define each form as a new object.

 

thatway all your variables can be kept track of via

 

$form->formname;

$form2->formname;

 

thats t he real usefulness of classes, your script however is best left unclassed.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.