Building Dynamic Web Pages with Polymorphism in PHP 5

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.

Introduction

If you already read the first article of the series, then it’s quite probable that the concept of working with polymorphic objects is now very familiar to you. However, in case this subject doesn’t ring any bells, let me offer a brief introduction. This will give you a more solid background for tackling the group of topics that I plan to cover in this article.

In short, Polymorphism is a feature exposed by certain objects that belong to the same family, which eventually can behave differently, even when they’re using identical methods. Or more clearly, an object can be considered polymorphic when it’s capable of performing different actions by utilizing the same method.

As you can see, this feature can be really useful in certain situations where using polymorphic objects can improve noticeably the way that a specific application is structured, aside from avoiding the inclusion into the source code of redundant checking blocks, such as the popular “if” and “switch/case” combinations.

The previous theory was clearly demonstrated in the preceding article, where I showed how to take advantage of polymorphism by building a simple yet expansible database abstraction layer for working with MySQL and SQLite. Obviously, the aforementioned  layer used polymorphic objects for accessing both database systems, but this concept can be easily extended to other applications, which makes it even more interesting.

Therefore, bearing in mind that the implementation of polymorphic objects in PHP 5 can be indeed as useful to you as it is to me, in this second part of the series, I’m going to show you how to use polymorphism to build dynamic web pages.

The experience not only will be interesting, but also educational, so let’s not waste more time in preliminaries. 

{mospagebreak title=Building web pages using Polymorphism}

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.

{mospagebreak title=Extending the concept of polymorphic classes}

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.

{mospagebreak title=Seeing some polymorphic objects in action}

As I stated in the section that you just read, at this moment I’d like to create an educational example where all the classes that were defined previously are put to work in conjunction. As you’ll see in the next few lines, the purpose of developing this example is to demonstrate how a small set of polymorphic objects can be used to create a complete web document.

But, I don’t want to bore you with irrelevant details any longer, therefore pay attention to the following code sample, please:  

try{
   // build array of web page objects
  $elements=array(new H1(‘This is an H1 header’,”,”),new H2
(‘This is an H2 header’,”,”),new H3(‘This is an H3
header’,”,”),new Paragraph(‘This is a
paragraph’,’parid’,’parclass’),new Div(‘This is a
DIV’,’divid’,’divclass’),new Link(‘This is a
link’,’linkid’,’linkclass’,’default,htm’));
   // display web page elements using polymorphism
   foreach($elements as $element){
     echo $element->display();
   }
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

Even when the signature of the above script is short, it demonstrates in a nutshell how polymorphism can be used to generate a simple web page. Our page includes three headers and one paragraph, one containing DIV and finally a regular link.

Nonetheless, the most interesting detail to notice here is how the same “display()” method is called by the different web page objects within a “foreach” construct to build the web document in question. Precisely, this condition is demonstrated by the following code block:

foreach($elements as $element){
  echo $element->display();
}

All right, now that you have learned how to take advantage of polymorphism, I suggest you start using this fundamental pillar of object-oriented programming when you build your own PHP applications.

Final thoughts

Sad but true, we’ve come to the end of this tutorial. In this second installment of the series, I provided you with a clear example of how to use polymorphic objects to build dynamic web documents.

However, if you think this educational journey has finished, I’m afraid you’re wrong. There’s still one last article to read, where I’ll explain how to utilize the benefits of polymorphism to validate user-supplied data. You’ve been warned, so don’t miss it!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan