PHP
  Home arrow PHP arrow Page 3 - Completing a Web Site Template using I...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Completing a Web Site Template using Inheritance in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2007-07-17

    Table of Contents:
  • Completing a Web Site Template using Inheritance in PHP 5
  • Listing the signatures of the web site's base classes
  • Extending the usage of inheritance
  • Completing the development of the sample web site

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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.

    More PHP Articles
    More By Alejandro Gervasio


       · Over the course of this final article of the series, an entire, fictional web site ...
       · disappointing. sorry.I see no need to derive a new class for every page. Should...
       · Thank you for your comments on my PHP article. I found your suggestions very...
     

       

    PHP ARTICLES

    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway