PHP
  Home arrow PHP arrow Page 2 - 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 
VeriSign Whitepapers 
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 - 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


       · 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

    - 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...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





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