PHP
  Home arrow PHP arrow Page 3 - Building Dynamic Web Pages with Polymo...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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

Building Dynamic Web Pages with Polymorphism in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2007-03-28

    Table of Contents:
  • Building Dynamic Web Pages with Polymorphism in PHP 5
  • Building web pages using Polymorphism
  • Extending the concept of polymorphic classes
  • Seeing some polymorphic objects in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Building Dynamic Web Pages with Polymorphism in PHP 5 - Extending the concept of polymorphic classes


    (Page 3 of 4 )

    In accordance with the concepts that were deployed in the previous section, after creating the corresponding classes that display some (X)HTML headers, the next step that I'm going to take now involves deriving some additional subclasses from the parent "WebPageElement." These will be responsible for rendering different web page elements, in this case including paragraphs, divs and some regular links.

    The respective signatures for these classes are listed below, so I suggest you take a look at them:

    // define concrete 'Paragraph' class
    class Paragraph extends WebPageElement{
       public function __construct($data,$id,$class){
         parent::__construct($data,$id,$class);
       }
       public function display(){
         $html='<p';
         if(!empty($this->id)){
           $html.=' id="'.$this->id.'"';
         }
         if(!empty($this->class)){
           $html.=' class="'.$this->class.'"';
         }
         $html.='>'.$this->data.'</p>';
         return $html;
       }
    }

    // define concrete 'Div' class
    class Div extends WebPageElement{
       public function __construct($data,$id,$class){
         parent::__construct($data,$id,$class);
       }
       public function display(){
         $html='<div';
         if(!empty($this->id)){
           $html.=' id="'.$this->id.'"';
         }
         if(!empty($this->class)){
           $html.=' class="'.$this->class.'"';
         }
         $html.='>'.$this->data.'</div>';
         return $html;
       }
    }

    // define concrete 'Link' class
    class Link extends WebPageElement{
       private $href;
       public function __construct($data,$id,$class,$href){
         parent::__construct($data,$id,$class);
         $this->href=!empty($href)?$href:'#';
       }
       public function display(){
         $html='<a';
         if(!empty($this->id)){
           $html.=' id="'.$this->id.'"';
         }
         if(!empty($this->class)){
           $html.=' class="'.$this->class.'"';
         }
         $html.=' href="'.$this->href.'">'.$this->data.'</a>';
         return $html;
       }
    }

    As you can see, the three classes listed above look closely similar to the ones shown in the previous section. However, these are tasked with displaying paragraphs, divs and regular links, and they all implement its "display()" method differently. Again, I'm working with polymorphic objects. Pretty simple, isn't it?

    All right, at this stage I already defined a decent number of subclasses whose purpose is simply displaying web page elements, meaning that it's possible to use them to create a complete web document. Of course, the best point of all is that this process can be achieved by taking advantage of polymorphism.

    In the next section I'm going to set up an illustrative example to show you clearly how a simple web page can be generated by using all the classes that I built previously.

    To learn how this practical example will be developed, please click on the link that appears below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · As you learned in the first article of the series, the implementation of polymorphic...
       · Alejandro, this article is Great !
       · Thank you for the compliments on my PHP article. I hope it's been useful to you and...
       · Thanks for your Great Work.This is very useful to New Php...
       · Thank you for commenting on my PHP article, and I'm glad to know it's been useful to...
     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway