Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Completing a Web Site Template using Inheritance in PHP 5
  2. Listing the signatures of the web site's base classes
  3. Extending the usage of inheritance
  4. Completing the development of the sample web site
By: Alejandro Gervasio
Rating: starstarstarstarstar / 14
July 17, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap

Dev Shed Tutorial Topics: