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.
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.
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!