PHP
  Home arrow PHP arrow Page 3 - Completing a Web Site Template using Inheritance in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Completing a Web Site Template using Inheritance in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 14
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek