Академический Документы
Профессиональный Документы
Культура Документы
By icarus
This article copyright Melonfire 20002002. All rights reserved.
Back To Class
Table of Contents
OOPs!...................................................................................................................................................................1 Who Needs Class, Anyway?...............................................................................................................................2 Driving Around The Bend ..................................................................................................................................3 If Wishes Were Prancing Horses.......................................................................................................................4 New Cars For Old...............................................................................................................................................5 Ford's Law...........................................................................................................................................................6 Turning The Tables............................................................................................................................................7 Under Construction............................................................................................................................................9 Extending Yourself...........................................................................................................................................10 Be My Guest......................................................................................................................................................11
OOPs!
If you've been programming for a while, you've probably heard the term OOP before it stands for ObjectOriented Programming, and refers to a technique in which you create program "objects" and then use these objects to build the functionality you need into your program. And maybe you've wondered just how useful this is, and how you could go about adding this capability to your own code. Well, wonder no more. Your prayers have been answered. Over the next few pages, this article is going to take a look at OOP in PHP4, with an explanation of PHP4 classes and a few examples of how you could use them.
OOPs!
As the illustration above shows, once new objects are defined, their individual methods and variables can be accessed and modified independent of each other. This comes in very handy, as the next few pages will show.
Every class definition begins with the keyword "class", followed by a class name. You can give your class any name that strikes your fancy, so long as it doesn't collide with a reserved PHP word. A pair of curly braces encloses all class variables and functions, which are written as you would normally code them. In order to create a new instance of a class, you need to use the "new" keyword, and assign the newlycreated object to a PHP variable. <? $his = new Automobile; ?>
In English, the above would mean "create a new object of class Automobile and assign it to the variable $his". You can now access all the methods and properties of the class via this variable. <? // accessing properties $his>make = "Ferrari"; $his>colour = "Black"; // accessing methods $his>start(); $his>accelerate(180); ?>
would mean "assign the value Ferrari to the variable $make of this specific instance of the class Automobile", while $his>accelerate(180);
would mean "execute the function accelerate() with parameter 180 of this specific instance of the class Automobile". Note the > symbol, and the fact that the $ symbol is omitted when accessing variables of a specific class instance.
Ford's Law
In case you need to access functions or modify variables within the class definition itself, PHP offers the $this keyword, which is used to refer to "this" class. For example, if you were to add Henry Ford's edict of "any colour, so long as it's black" to the definition above, you'd end up with something like this: class Automobile { // this is where the properties are defined var $colour; var $shape; ... // this is where the functions are defined function start() ( // code goes here ) function turnColourBlack() { $this>colour = "Black"; } ... }
In this case, the $this prefix indicates that the variable to be modified is included within the current class definition or, in English, "change the value of the variable $colour within this class to Black". The $this prefix thus provides a convenient way to access variables and functions which are "local" to the class.
Ford's Law
If you take a close look, you'll see that this class contains three functions one to set the number of rows and columns, one to decide the foreground and background colours and the font, and one to actually draw the table. Note the frequent usage of the $this keyword to reference local variables within the class. Now, to use this, all you need to do is include this class definition (which you could save to a separate file named "tables.inc") within your PHP script. <? // this is a PHP page which will contain many tables // include the class definition include("tables.inc"); ?> <html> <head> <basefont face=Arial> </head> <body>
Here is the first table. <? // first table $alpha = new Table; $alpha>setGrid(4,4); $alpha>setInterface("green", "white", "Arial"); $alpha>drawTable(); ?> <p>
And here's the second. <? // second table $beta = new Table; $beta>setGrid(1,9); $beta>setInterface("blue", "white", "Arial Black"); $beta>drawTable(); ?> <p>
Back To Class <? // third table $gamma = new Table; $gamma>setGrid(2,4); $gamma>setInterface("black", "white", "Verdana"); $gamma>drawTable(); // fourth table $omega = new Table; $omega>setGrid(4,2); $omega>setInterface("white", "black", "Trebuchet MS"); $omega>drawTable(); ?> </body> </html>
Under Construction
It's also possible to automatically execute a function when the class is called to create a new object. This is referred to in geek lingo as a "constructor" and, in order to use it, your class definition must contain a function with the same name as the class. For example, if you'd like your car to start automatically when you create it, add the function Automobile() to your definition, as show below. <? class Automobile { // constructor function Automobile { $this>start(); } function start() ( // code goes here ) } ?>
Or, in the table example you just saw, you could define a default grid and colours for your table when the object is created. <? class Table { // constructor function Table() { $this>rows = 4; $this>columns = 5; $this>bcolor = "black"; $this>fcolor = "white"; $this>font = "Times New Roman"; } // other functions ?>
And now, when you create a new Table object like this, without defining any parameters, <? // first table $alpha = new Table; $alpha>drawTable(); ?>
you'll get a default 4x4 black and white grid. Try it and see for yourself!
Under Construction
Extending Yourself
Two of the best things about OOP are extensibility and inheritance. Very simply, this means that you can create a new class based on an existing class, add new features (read properties and methods) to it, and then create objects based on this new class. These objects will contain all the features of the original parent class, together with the new features of the child class. As an illustration, consider the following evenBetterTable class, which adds support for cell spacing and borders. <? class Table { // constructor function Table() { $this>rows = 4; $this>columns = 5; $this>bcolor = "black"; $this>fcolor = "white"; $this>font = "Times New Roman"; } // set the rows and columns function setGrid($rows, $columns) { $this>rows = $rows; $this>columns = $columns; } // set the colours and fonts function setInterface($bcolor, $fcolor, $font) { $this>bcolor = $bcolor; $this>fcolor = $fcolor; $this>font = $font; } // draw the table function drawTable() { echo "<table cellspacing=" . $this>spacing . " border=" . $this>bsize . " bgcolor=" . $this>bcolor . ">"; for ($x=1; $x<=$this>rows;$x++) { echo "<tr>"; for ($y=1; $y<=$this>columns;$y++) { echo "<td><font face=\"" . $this>font . "\" color=" . $this>fcolor . ">" . $x . ", " . $y . "</font></td>"; } echo "</tr>"; } echo "</table>"; } } class evenBetterTable extends Table { // set cell spacing function setSpacing($space) { $this>spacing = $space; } // set borders function setBorder($size) { $this>bsize = $size; } } ?>
The "extends" keyword is used to extend a parent class to a child class. All the functions and variables of the parent class immediately become available to the child class. And here's how you could use it: <? // this is a PHP page which demonstrates extensibility and inheritance // include the class definition include("tables.inc"); ?> <html> <head> <basefont face=Arial> </head> <body> <? // first table $alpha = new evenBetterTable; $alpha>setGrid(4,4); $alpha>setInterface("yellow", "black", "Arial Black"); $alpha>setSpacing(10); $alpha>setBorder(2); $alpha>drawTable(); ?> </body> </html>
Extending Yourself
10
Be My Guest
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
Be My Guest
11
Back To Class 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!
Be My Guest
12