PHP
  Home arrow PHP arrow Page 4 - Building Object-Oriented Web Pages with Inheritance in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Building Object-Oriented Web Pages with Inheritance in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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!



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek