Home arrow PHP arrow Page 9 - Back To Class

Be My Guest - PHP

Classes and objects are powerful OOP concepts - and PHP4 supportsthem too. This article explains some basic OO entities (including classes,constructors and extensibility) with examples of a table builder and aguestbook.

TABLE OF CONTENTS:
  1. Back To Class
  2. Who Needs Class, Anyway?
  3. If Wishes Were Prancing Horses...
  4. New Cars For Old
  5. Ford's Law
  6. Turning The Tables
  7. Under Construction
  8. Extending Yourself
  9. Be My Guest
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 9
October 05, 2000

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
And finally, here's another, slightly more complex example which demonstrates how powerful classes can be. It's a Guestbook class, and it includes support for most of the common functions available in a guestbook.

Now, how do you use it? Well, first you need a form like this, which serves as the data entry page for the guestbook.

<html> <head> <title>Sign My Guestbook</title> </head> <body> <form action="book.php4" method="post"> <center> <table width="600" cellpadding="10" cellspacing="5" > <tr> <td width="300" align="right">Name</td> <td width="300" align="left"><input type="text" name="name" size="25" maxlength="25"></td> </tr> <tr align="center"> <td width="300" align="right">Email Address</td> <td width="300" align="left"><input type="text" name="email" size="25" ></td> </tr> <tr> <td align="right" width="300">Comments</td> <td align="left" width="300"><textarea name="comments" cols="25" rows="3" wrap="virtual"></textarea></td> </tr> <tr> <td align="center" colspan=2 width="600"><input type="submit" value="Sign my guestbook"></td> </tr> </table> </center> </form> </body> </html>
As you can see, the form data will be submitted to a PHP script called "book.php4" - let's take a look at that next.

<?php // this is book.php4 - it accepts form data and writes to a file // Guestbook class included include("guestbook.inc"); // spawn a guestbook $mybook = new Guestbook(); // set an object property // make sure you have permission to write this file $mybook->usefile('melonfire.dat'); // use a method of the new object to write data if($name && $email && $comments) { $mybook->add_entry($name,$email,$comments); } // object also includes a method to display previous entries $mybook->display(); ?>
This script creates a new Guestbook object, specifies the file to use for the data, and then adds an entry to it. Once that's done, it calls a display() function to display the entries in the book. Needless to say, all these functions have been created in the definition of the Guestbook class. And now for the definition itself - take a look:

<?php class Guestbook{ // default settings function Guestbook() { $this->title = "My Guestbook"; $this->fontface = "Verdana"; $this->fontsize = "2"; $this->fontcolor = "#FF0000"; $this->filename = "default.txt"; } //set the title function set_title($title) { $this->title = $title; } //set the font properties function set_fontsize($fontsize) { $this->fontsize = $fontsize; } function set_fontface($fontface) { $this->fontface = $fontface; } function set_fontcolor($fontcolor) { $this->fontcolor = $fontcolor; } // set the name of the data file function usefile($file) { $this->filename = $file; } // function to actually write form data to file // elements of each entry are separated by a | function add_entry($name,$email,$comments) { $entry = $name."|".$email."|".$comments."\n"; $this->fpointer = fopen($this->filename,"a+"); fputs($this->fpointer,$entry); fclose($this->fpointer); } // split entries against | // and call display_entries() function function split_entries($file) { $entries = file($file,"r"); for($counter = 0; $counter < sizeof($entries); $counter++) { $entry = explode ("|", $entries[$counter]); $this->display_entries($entry); } } // display an entry in the guestbook function display_entries($entry) { for($counter = 0;$counter < sizeof($entry);$counter++) { print "<center><font face=\"$this->fontface\" size=$this->fontsize color=$this->fontcolor>$entry[$counter]</font></center><br>"; } print "<hr width=50%>"; } // display the title function display_title() { print "<center><font face=\"$this->fontface\" size=$this->fontsize color=$this->fontcolor>$this->title</font></center><br><hr width=75%>"; } // display page function display() { $this->display_title(); $this->split_entries($this->filename); } } ?>
As you can see, the definition includes functions to control the interface of the guestbook, in addition to functions to read and write data. If you ever decide to go into the guestbook services business, you'd probably offer your users something quite similar in order to allow them to add guestbook services to their Web site (as many of the free Web space providers out there already do).

And that just about covers classes and objects in PHP. See you soon!

 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap

Dev Shed Tutorial Topics: