Home arrow PHP arrow Page 2 - Building Dynamic Web Pages with Polymorphism in PHP 5

Building web pages using Polymorphism - PHP

If you’re starting to delve deeper into object-oriented programming with PHP, and also want to know how to include polymorphic objects into your own scripts, this might be the right opportunity to learn more about this interesting topic. Welcome to the second part of the series that began with “Using polymorphism with objects in PHP 5.” In three articles, this series provides you with a comprehensive guide on how to take advantage of polymorphism to build more efficient object-based PHP applications.

TABLE OF CONTENTS:
  1. Building Dynamic Web Pages with Polymorphism in PHP 5
  2. Building web pages using Polymorphism
  3. Extending the concept of polymorphic classes
  4. Seeing some polymorphic objects in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 15
March 28, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 



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

Dev Shed Tutorial Topics: