HomePHP Page 4 - Completing a Web Site Template using Inheritance in PHP 5
Completing the development of the sample web site - PHP
If you're a PHP developer who develops object-oriented applications on a frequent basis, then you'll know that inheritance can be an extremely useful concept that might help in reusing code when it comes to creating a hierarchy of classes. This article, and yesterday's, take advantage of this powerful concept and show you how to use some straightforward classes to build a complete web site that presents a consistent look across its pages.
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:
// 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.