PHP
  Home arrow PHP arrow Page 8 - 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 - Extending Yourself
    ( Page 8 of 9 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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