HomePHP 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.
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
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:
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.