Completing a Web Site Template using Inheritance in PHP 5 - Extending the usage of inheritance (
Page 3 of 4 )
As I mentioned in the previous section, I'm now going to create a classic "Products" web page by deriving a subclass from the "HomeWebPage" class that you saw earlier.
This being said, here is the definition for the brand new "Products" web page:
class ProductWebPage extends HomeWebPage{
private $mainLinks=array('Home'=>'index.php','About
us'=>'about.php','Products'=>'products.php','Contact'=>
'contact.php');
private $subLinks=array('PHP
Software'=>'phpsoftware.php','JavaScript
libraries'=>'jslibraries.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 shown above, the "ProductWebPage" class has been created as a child class of the initial "HomeWebPage," which demonstrates in a nutshell how inheritance can be used in a simple way to build the diverse sections of a given web site.
Now that you have seen the signature of the "ProductWebPage," please pay careful attention to the following script, which is tasked with displaying the "Products" web page in question:
try{
// instantiate 'ProductWebPage' class
$productPage=new ProductWebPage('Products','This is the default content for PRODUCTS page');
// display web page
echo $productPage->buildMetaData();
echo $productPage->buildStyles();
echo $productPage->buildHeader();
echo $productPage->buildLinks();
echo $productPage->buildBody();
echo $productPage->buildFooter();
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
As you can see, building a specific web page for this fictional web site is reduced to deriving a subclass from the corresponding parent, and overriding/overloading one or more of its methods, according to the requirements of that particular section.
Finally, I'd like to show you the complete signature of the basic "Products" web page that was built earlier, this time including the respective parent classes:
(definition for products.php file)
<?php
try{
// include parent classes
require_once 'abstract_webpage.php';
require_once 'homewebpage.php';
require_once 'productswebpage.php';
// instantiate 'ProductWebPage' class
$productPage=new ProductWebPage('Products','This is the
default content for PRODUCTS page');
// display web page
echo $productPage->buildMetaData();
echo $productPage->buildStyles();
echo $productPage->buildHeader();
echo $productPage->buildLinks();
echo $productPage->buildBody();
echo $productPage->buildFooter();
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
?>
Okay, now that you have grasped the approach used in creating the "Products" web page, let's leap forward and see how to build the "Contact" section of this sample web site, thus completing the development of its basic structure.
To see how this last web page will be built, please read the next few lines. Don't worry, since I'll be there, waiting for you.