HomePHP 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.
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.