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

Extending the concept of polymorphic classes - 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

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: