Home arrow PHP arrow Page 4 - Building Object-Oriented Web Pages with Inheritance in PHP 5

Building another web page for a fictional web site - PHP

You have probably used the principles of inheritance in any number of your object-oriented programming projects. Traditionally this means working with parent and child classes during the creation of a PHP application. In this two-part series, you'll learn a simple way to use inheritance while creating object-oriented web pages.

  1. Building Object-Oriented Web Pages with Inheritance in PHP 5
  2. Establishing a basic class hierarchy
  3. Using inheritance to derive a web page generator class
  4. Building another web page for a fictional web site
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
July 16, 2007

print this article



As I stated in the previous section, building a different web page for this sample web site is only a matter of deriving another child class from the "HomeWebPage" (not from the base class), since it has already implemented the business logic required to display a complete web document.

So, bearing in mind this concept, below I included the definition of a brand new subclass, which displays the "About Us" section of this fictional web site.

Having said that, here's the signature of this child class:

class AboutUsWebPage extends HomeWebPage{
   private $mainLinks=array('Home'=>'index.php','About
   private $subLinks=array('Our company profile'=>'company.php','What customers are saying'=>'customers.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";
     foreach($this->subLinks as $label=>$link){
       $output.='<li><a href="'.$link.'">'.$label.'</a></li>'."n";
     return $output;

As you can see, the above "AboutUsWebPage" class only overrides the initial "buildLinks()" method implemented by its corresponding parent, with the purpose of displaying a few additional secondary links on the browser. However, as you might have guessed, the main advantage of this approach rests with using inheritance to create quickly the different sections of this sample web site. Quite simple, right?

Logically, if you're anything like me, you might want to see the signature of the source files that display the respective "Home" and "About Us" sections of this sample web site. Below I listed the definitions of these files, in this way demonstrating how a few inherited classes can be used to build a complete object-oriented web site.

Given that, here are the source files in question:

(definition for index.php file)


   // include parent classes
require_once 'abstract_webpage.php';
require_once 'homewebpage.php';

   // 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();

(definition for about.php file)

   // include parent classes 
require_once 'abstract_webpage.php';
require_once 'homewebpage.php';
require_once 'aboutwebpage.php';

   // instantiate 'AboutUsWebPage' class
   $aboutPage=new AboutUsWebPage('About us','This is the default
content for ABOUT US page');
   // display web page
   echo $aboutPage->buildMetaData();
   echo $aboutPage->buildStyles();
   echo $aboutPage->buildHeader();
   echo $aboutPage->buildLinks();
   echo $aboutPage->buildBody();
   echo $aboutPage->buildFooter();
catch(Exception $e){
   echo $e->getMessage();

As you can see, building the different sections of this sample web site is a process reduced to including the required parent classes and instantiating the concrete class that displays a particular web page. Of course, in this case the functionality provided by inheritance is actually what makes creating distinct web documents a no-brainer process.

Finally, I suggest that you try creating your own web page generator classes, so you can understand more easily the way that inheritance can assist you in building dynamic web pages.

Final thoughts

In this first installment of the series, I hopefully illustrated how inheritance can be utilized in a helpful fashion to build the different web pages of a sample web site, whose look and feel is nearly the same across its respective sections.

However, this instructive journey hasn't ended yet. In the last part I'm going to show you how to create some additional web pages to complete the structure of this fictional web site.

Now that I've told you about the topics that will be covered in the final article, I'm sure you won't want to miss it!

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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