Completing a Web Site Template using Inheritance in PHP 5 - Completing the development of the sample web site
(Page 4 of 4 )
In consonance with the concepts expressed in the section that you just read, the last thing that I'm going to show you in this article will consist of building a typical "Contact" web page for the sample web site that you learned before.
Basically, the process required to create the web page is based upon deriving a subclass from the respective "HomeWebPage" parent, as I did previously with the other web site sections; by this time, that concept that shouldn't be hard to grasp for you at all.
Now that I have explained how this brand new class will be defined, please focus your attention on the following code sample, which shows its signature:
class ContactWebPage extends HomeWebPage{
private $mainLinks=array('Home'=>'index.php','About
us'=>'about.php','Products'=>'products.php','Contact'=>
'contact.php');
private $subLinks=array('Email
contact'=>'email.php','HeadQuarters'=>'headquarters.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;
}
}
I won't stop for long to explain how the above class works, since its logic is nearly identical to the examples shown in the previous sections. Instead, I'm going to list the complete definition of a hypothetical "Products" web page, including the required parent classes:
That being said, here's how this PHP file looks:
<?php
try{
// include parent classes
require_once 'abstract_webpage.php';
require_once 'homewebpage.php';
require_once 'contactwebpage.php';
// instantiate 'ContactWebPage' class
$contactPage=new ContactWebPage('Contact','This is the default
content for CONTACT page');
// display web page
echo $contactPage->buildMetaData();
echo $contactPage->buildStyles();
echo $contactPage->buildHeader();
echo $contactPage->buildLinks();
echo $contactPage->buildBody();
echo $contactPage->buildFooter();
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
?>
As demonstrated above, that's all the PHP code required to build a basic "Contact" web page, thanks to the capacity provided by inheritance. Please feel free to modify all the sample classes shown here, so you can develop your own examples for testing, and eventually acquire a better grounding in how to work with inherited classes in PHP 5.
Final thoughts
Sadly, this is the end of the series. As you have learned, inheritance is indeed a very powerful concept that allows, among other neat things, the development of object-oriented web sites. Of course, for larger applications the approach shown here isn't very suitable, but for small ones, the implementation of this technique, along with a simple template system, might be a pretty efficient solution.
See you in the next PHP tutorial!
| 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. |