PHP
  Home arrow PHP arrow Page 2 - 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? 
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 - Listing the signatures of the web site's base classes
    ( Page 2 of 4 )

    Before I start demonstrating how to incorporate the "Products" and "Contacts" sections into the sample web site, I'd like to list once again the corresponding definitions of the base classes that behave like the real workhorses of this web site generating application.

    Given that, here are the code samples for the original classes. Please examine them. 

    // define abstract 'WebPage' class
    abstract class WebPage{
       private $html;
       private $title;
       private $mainLinks;
       private $content;
       abstract public function __construct($title,$content);
       abstract public function buildMetaData();
       abstract public function buildStyles();
       abstract public function buildHeader();
       abstract public function buildLinks();
       abstract public function buildBody();
       abstract public function buildFooter();
    }
    // define concrete 'HomeWebPage' class
    class HomeWebPage extends WebPage{
       // define default values for page links
       private $mainLinks=array('Home'=>'index.php','About
    us'=>'about.php','Products'=>'products.php','Contact'=>
    'contact.php');
       public function __construct($title='Home Page',$content='This
    is the default context for HOME web page'){
         if(!$title||is_numeric($title)||strlen($title)>256){
           throw new Exception('Invalid title for the web page.');
         }
         if(!$content){
           throw new Exception('Invalid contents for the web page.');
         }
         $this->title=$title;
         $this->content=$content;           
       }
       // implement 'buildMetaData()' method
       public function buildMetaData(){
    $output=<<<EOD

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />

    EOD;
         return $output;
       }
       // implement 'buildStyles()' method
       public function buildStyles(){
    $output=<<<EOD

    <style type="text/css">
    body{
       padding: 0;
       margin: 0;
       background: #fff;
    }

    h2{
       margin: 0;
       font: bold 18px Arial, Helvetica, sans-serif;
       color: #000;      
    }

    p{
       font: normal 12px Arial, Helvetica, sans-serif;
       color: #000;      
    }

    #header{
       padding: 2%;
       background: #ffc;
    }

    #navbar{
       padding: 1% 2% 1% 2%;
       background: #fc0;
    }

    #navbar ul{
       list-style: none;
    }

    #navbar li{
       display: inline;
       padding-right: 4%;
    }

    #navbar a:link,#navbar a:visited{
       font: normal 12px Arial, Helvetica, sans-serif;
       color: #039;
       text-decoration: none;
    }

    #navbar a:hover{
       text-decoration: underline;
    }

    #mainwrapper{
       clear: both;
       height: 100%;
       overflow: hidden;
       background: #eee;
    }

    #mainwrapper .leftcol{
       position: relative;
       float: left;
    }

    #mainwrapper .rightcol{
       position: relative;
       float: right;
    }

    #leftbar{
       width: 16%;
       padding: 2%;
    }

    #centerbar{
       float: left;
       width: 55%;
       padding: 2%;
       background: #fff;
    }          

    #rightbar{
       width: 16%;
       padding: 2%;
    }

    #footer{
       clear: both;
       padding: 2%;
       background: #ffc;
    }
    </style>

    EOD;
         return $output;
       }
       // implement 'buildHeader()' method
       public function buildHeader(){
    $output.=<<<EOD

    <title>$this->title</title>
    </head>
    <body>
    <div id="header">
     
    <h2>This is the header section of the web page</h2>
     
    <p>Contents for header section go here.</p>
    </div>

    EOD;
         return $output;
       }
       // 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>'."n".'</div>';
         return $output;
       }
       // implement 'buildBody()' method
       public function buildBody(){
    $output=<<<EOD

    <div id="mainwrapper">
     
    <div id="leftbar" class="leftcol">
       
    <h2>This is the left column of the web page</h2>
       
    <p>Contents for left column go here..</p>
     
    </div>
     
    <div id="centerbar" class="leftcol">
       
    <h2>This is the center column of the web page</h2>
       
    <p>$this->content</p>
      
    </div>
     
    <div id="rightbar" class="rightcol">
       
    <h2>This is the right column of the web page</h2>
       
    <p>Contents for right column go here.</p>
     
    </div>
    </div>

    EOD;
         return $output;
       }
       // implement 'buildHeader()' method
       public function buildFooter(){
    $output.=<<<EOD

    <div id="footer">
     
    <h2>This is the footer section of the web page</h2>
     
    <p>Contents for footer section go here.</p>
    </div>
    </body>
    </html>

    EOD;
         return $output;
       }
    }

    Now that you hopefully recalled how the previous base classes worked, pay attention to the definition of the following PHP file, which was responsible for displaying the "home" page of this fictional web site:

    (definition for index.php file)

    <?php
    try{
       // instantiate 'HomeWebPage' class
       $homePage=new HomeWebPage();
       // display web page
       echo $homePage->buildMetaData();
       echo $homePage->buildStyles();
       echo $homePage->buildHeader();
       echo $homePage->buildLinks();
       echo $homePage->buildBody();
       echo $homePage->buildFooter();
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }
    ?>

    After studying the signature of the previous PHP, definitely you'll have to agree with me that building the different sections of a sample web site is actually a no-brainer process because of the functionality provided by inheritance.

    So far, so good, right? Now that you have hopefully recalled how to use the  "HomeWebPage" class to create the corresponding web page, I believe that it's a good time to move forward to see how the respective "Products" and "Contact" sections can be similarly generated by using most of the functionality of the base "HomeWebPage" class.

    Want to see how these brand new sections of this sample web site will be built? Click on the link that appears below and keep reading.



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT