PHP
  Home arrow PHP arrow Page 9 - Back To Class
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Back To Class
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2000-10-05

    Table of Contents:
  • Back To Class
  • Who Needs Class, Anyway?
  • If Wishes Were Prancing Horses...
  • New Cars For Old
  • Ford's Law
  • Turning The Tables
  • Under Construction
  • Extending Yourself
  • Be My Guest

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Back To Class - Be My Guest


    (Page 9 of 9 )

    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!
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT