PHP
  Home arrow PHP arrow Page 2 - 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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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 / 13
    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 - Building web pages using Polymorphism


    (Page 2 of 4 )

    As I expressed in the introduction, my main purpose of this article is to demonstrate how dynamic web documents can be created by applying the concept of polymorphic objects. To achieve this, I'm going to define on top of the corresponding hierarchy a base class, which will declare a couple of generic methods for rendering different types of web page elements.

    Next, I'm going to derive some subclasses from the aforementioned parent, which logically will implement concretely these generic methods. However, the question that comes up here is, how does polymorphism fit into this schema?

    Well, as you'll see in a moment, all these web document objects will present a method called "display()," which will perform different actions according to the type of web page element being displayed. In this way it will implement the so-called polymorphic objects.

    All right, now that you know how I'm going to use polymorphism to build dynamic web pages, please pay attention to the signature of the base class, shown below, which outlines the generic structure of a web page object. Its definition is as follows:

    // define 'WebPageElement' class
    class WebPageElement{
       protected $data;
       protected $id;
       protected $class;
       public function __construct($data,$id,$class){
         if(!$data){
           throw new Exception('Data for web page element must be a
    non-empty string.');
         }
         $this->data=$data;
         if($id){
           if(!preg_match("/^[a-z]+$/",$id)){
             throw new Exception('Invalid ID attribute.');
           }
           else{
             $this->id=$id;
           }
         }
         if($class){
           if(!preg_match("/^[a-z]+$/",$class)){
             throw new Exception('Invalid class attribute.');
           }
           else{
             $this->class=$class;
           }
         }
       }
       public function display(){}
    }

    As you can see, the above "WebPageElement" presents only the "display()" method, aside from the corresponding constructor. Also, the class in question accepts as input arguments, the ID and class attributes that will be used for rendering a specific web page element, in addition to the content that will be included inside this element.

    So far, so good. As you saw, the previous parent class only implements a primitive logic, useful for validating the different incoming arguments taken by the respective constructor. Next, I will derive some subclasses from the prior parent to display different web page elements.

    Having said that, here are the signatures corresponding to a group of brand new child classes, which are responsible for rendering <h1>, <h2> and <h3> headers respectively. Their definitions are as follows:

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

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

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

    As shown above, the three previous subclasses implement concretely the same "display()" method to generate the required markup that corresponds to the (X)HTML headers that I mentioned before.

    However, the most important thing to notice here concerns the different behaviors exposed by the method in question, since it performs different actions according to the type of web page object that calls it. Naturally, this fact precisely demonstrates that the previous subclasses are indeed polymorphic, since they belong to the same family, but behave differently. Pretty good, right?

    Okay, now that you know how these web page objects can be created by using polymorphism, I think it's a good time to move forward and define a few more web page subclasses, so they can be used later on to build a complete web document.

    As you know, all these brand new classes will be defined in the section to come, so jump ahead and read the next few lines. I'll be there, waiting for you.

    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

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT