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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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!

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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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