Home arrow PHP arrow Page 8 - Back To Class

Extending Yourself - 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
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>


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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap

Dev Shed Tutorial Topics: