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

Listing the signatures of the web site's base classes - 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

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
 

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: