Building Object-Oriented Web Pages with Inheritance in PHP 5 - Building another web page for a fictional web site
(Page 4 of 4 )
As I stated in the previous section, building a different web page for this sample web site is only a matter of deriving another child class from the "HomeWebPage" (not from the base class), since it has already implemented the business logic required to display a complete web document.
So, bearing in mind this concept, below I included the definition of a brand new subclass, which displays the "About Us" section of this fictional web site.
Having said that, here's the signature of this child class:
class AboutUsWebPage extends HomeWebPage{
private $mainLinks=array('Home'=>'index.php','About
us'=>'about.php','Products'=>'products.php','Contact'=>
'contact.php');
private $subLinks=array('Our company profile'=>'company.php','What customers are saying'=>'customers.php');
// implement 'buildLinks()' method
public function buildLinks(){
$output='<div id="navbar">'."n".'<h2>This is the navigation
bar of the web page</h2>'."n".'<ul>'."n";
foreach($this->mainLinks as $label=>$link){
$output.='<li><a href="'.$link.'">'.$label.'</a></li>'."n";
}
$output.='</ul><ul>';
foreach($this->subLinks as $label=>$link){
$output.='<li><a href="'.$link.'">'.$label.'</a></li>'."n";
}
$output.='</ul>'."n".'</div>';
return $output;
}
}
As you can see, the above "AboutUsWebPage" class only overrides the initial "buildLinks()" method implemented by its corresponding parent, with the purpose of displaying a few additional secondary links on the browser. However, as you might have guessed, the main advantage of this approach rests with using inheritance to create quickly the different sections of this sample web site. Quite simple, right?
Logically, if you're anything like me, you might want to see the signature of the source files that display the respective "Home" and "About Us" sections of this sample web site. Below I listed the definitions of these files, in this way demonstrating how a few inherited classes can be used to build a complete object-oriented web site.
Given that, here are the source files in question:
(definition for index.php file)
<?php
try{
// include parent classes
require_once 'abstract_webpage.php';
require_once 'homewebpage.php';
// instantiate 'HomeWebPage' class
$homePage=new HomeWebPage();
// display web page
echo $homePage->buildMetaData();
echo $homePage->buildStyles();
echo $homePage->buildHeader();
echo $homePage->buildLinks();
echo $homePage->buildBody();
echo $homePage->buildFooter();
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
(definition for about.php file)
try{
// include parent classes
require_once 'abstract_webpage.php';
require_once 'homewebpage.php';
require_once 'aboutwebpage.php';
// instantiate 'AboutUsWebPage' class
$aboutPage=new AboutUsWebPage('About us','This is the default
content for ABOUT US page');
// display web page
echo $aboutPage->buildMetaData();
echo $aboutPage->buildStyles();
echo $aboutPage->buildHeader();
echo $aboutPage->buildLinks();
echo $aboutPage->buildBody();
echo $aboutPage->buildFooter();
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
As you can see, building the different sections of this sample web site is a process reduced to including the required parent classes and instantiating the concrete class that displays a particular web page. Of course, in this case the functionality provided by inheritance is actually what makes creating distinct web documents a no-brainer process.
Finally, I suggest that you try creating your own web page generator classes, so you can understand more easily the way that inheritance can assist you in building dynamic web pages.
Final thoughts
In this first installment of the series, I hopefully illustrated how inheritance can be utilized in a helpful fashion to build the different web pages of a sample web site, whose look and feel is nearly the same across its respective sections.
However, this instructive journey hasn't ended yet. In the last part I'm going to show you how to create some additional web pages to complete the structure of this fictional web site.
Now that I've told you about the topics that will be covered in the final article, I'm sure you won't want to miss it!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |