PHP
  Home arrow PHP arrow Page 4 - Building Object-Oriented Web Pages wit...
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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

Building Object-Oriented Web Pages with Inheritance in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2007-07-16

    Table of Contents:
  • Building Object-Oriented Web Pages with Inheritance in PHP 5
  • Establishing a basic class hierarchy
  • Using inheritance to derive a web page generator class
  • Building another web page for a fictional 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


    Building Object-Oriented Web Pages with Inheritance in PHP 5 - Building another web page for a fictional web site


    (Page 4 of 4 )

    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
    us'=>'about.php','Products'=>'products.php','Contact'=>
    'contact.php');
       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";
         }
         $output.='</ul><ul>';
         foreach($this->subLinks as $label=>$link){
           $output.='<li><a href="'.$link.'">'.$label.'</a></li>'."n";
         }
         $output.='</ul>'."n".'</div>';
         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)

    <?php

    try{
       // 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();
       exit();
    }

    (definition for about.php file)

    try{
       // 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();
       exit();
    }

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · In this first tutorial of the series, the concept od inheritance is applied, in...
       · Why would you not put sub-link functionality in the base class???If you know...
       · Thank you for posting your opinions with regard to my PHP article, and sorry to read...
       · for someone new to obo such as myself i found this very helpful, can you tell me how...
       · Thanks for the comments on my PHP article. Regarding your question, you should...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





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