Page 1 of 1

guestbook code: help with classes

Posted: Mon Dec 29, 2003 6:30 pm
by thomasd1
could someone please rewrite an example of how a class could be used in this lil script from my guestbook?

Code: Select all

<?php require 'db_info.php'; ?>
<?php
if (isset($_GET["name"]) && isset($_GET["email"]) && isset($_GET["message"])) {
if ( $_GET["name"]!="" && $_GET["email"]!="" && $_GET["message"]!=""){
//prepare variables:
$name = htmlentities($_GET["name"]);
$email = htmlentities($_GET["email"]);
$datetime = date("Y.m.d g:i:s");
$message = nl2br(htmlentities($_GET["message"]));
//send to DB
$db = mysql_connect($dbhost,$dbuser);	//DB link_identifier
mysql_select_db($mydb,$db);	//Select DB
$query = "INSERT INTO guestbook(id, name, email, datetime, message) VALUES(NULL, '$name', '$email', '$datetime', '$message')";
$result = mysql_query($query);
header("Location: index.php");
} else {
header("Location: sign.php");
}
} else {
header("Location: index.php");
}
?>
i really have no clue what classes are used for, i hear they can make scripts more easy, i really don't understand how ..? :|

Posted: Mon Dec 29, 2003 8:23 pm
by DuFF
Sorry but we're not here to write code for you. What we can do is help you with any problems you're having and point you in the right direction.

First of all, if that is all the code you have for the guestbook then there is really no reason to use OOP (Object Oriented Programming, which includes classes, objects and methods). Classes are generally used in bigger applications as a way to get around having to reuse large portions of code over and over again. One good thing about classes is that they are very easy to reuse in different projects, so you could have a class that works with your database and use it in any project that needs a database.

Here's a great list of PHP OOP tutorials:
http://www.faqts.com/knowledge_base/view.phtml/aid/7569

Posted: Mon Dec 29, 2003 9:58 pm
by McGruff
Classes are all about encapsulating complexity behind a simple interface (the class's public methods). It's easier to understand things broken down into chapters and pages rather than try to hold a whole book in your head.

First of all, learn how to use functions well since you can't write good classes without good functions.

Ideally a function should do one thing well (same for classes). Responsibility driven design strives to separate out responsibilities for specific actions into different functions or objects and so makes the code much more modular and easier to work with. Some functions might not "do" anything at all but merely call other functions, thus acting as controllers (and nothing else).

For example your code might be re-written with a function to validate the submitted data (POST is the preferred form method by the way), and a function to update the database (always escape strings). That just leaves the business of deciding what to do (redisplay form, or process submitted data & serve up the success page) which again might be wrapped up in a function.