Home arrow PHP arrow Page 6 - Back To Class

Turning The Tables - 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
Let's take a look at an example which illustrates how useful classes can actually be. This next class definition allows you to specify the number of rows and columns you would like, and then creates an HTML table for you on the basis of your selections.

<? class Table { // 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 border=1 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>"; } } ?>
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>
Third and fourth go together.

<? // 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>
Pretty cool, huh?

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: