Home arrow PHP arrow Page 3 - Completing a Web Site Template using Inheritance in PHP 5

Extending the usage of inheritance - 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

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.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: