PHP
  Home arrow PHP arrow Page 2 - Building Dynamic Web Pages with Polymorphism in PHP 5
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? 
Google.com  
PHP

Building Dynamic Web Pages with Polymorphism in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek