PHP
  Home arrow PHP arrow Page 4 - Completing a Web Site Template using I...
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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

Completing a Web Site Template using Inheritance in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2007-07-17

    Table of Contents:
  • Completing a Web Site Template using Inheritance in PHP 5
  • Listing the signatures of the web site's base classes
  • Extending the usage of inheritance
  • Completing the development of the sample 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Completing a Web Site Template using Inheritance in PHP 5 - Completing the development of the sample web site


    (Page 4 of 4 )

    In consonance with the concepts expressed in the section that you just read, the last thing that I'm going to show you in this article will consist of building a typical "Contact" web page for the sample web site that you learned before.

    Basically, the process required to create the web page is based upon deriving a subclass from the respective "HomeWebPage" parent, as I did previously with the other web site sections; by this time, that concept that shouldn't be hard to grasp for you at all.

    Now that I have explained how this brand new class will be defined, please focus your attention on the following code sample, which shows its signature:

    class ContactWebPage extends HomeWebPage{
       private $mainLinks=array('Home'=>'index.php','About
    us'=>'about.php','Products'=>'products.php','Contact'=>
    'contact.php');
       private $subLinks=array('Email
    contact'=>'email.php','HeadQuarters'=>'headquarters.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;
       }
    }

    I won't stop for long to explain how the above class works, since its logic is nearly identical to the examples shown in the previous sections. Instead, I'm going to list the complete definition of a hypothetical "Products" web page, including the required parent classes:

    That being said, here's how this PHP file looks:

    <?php

    try{
       // include parent classes
      
    require_once 'abstract_webpage.php';
      
    require_once 'homewebpage.php';
      
    require_once 'contactwebpage.php';

       // instantiate 'ContactWebPage' class
       $contactPage=new ContactWebPage('Contact','This is the default
    content for CONTACT page');
       // display web page
       echo $contactPage->buildMetaData();
       echo $contactPage->buildStyles();
       echo $contactPage->buildHeader();
       echo $contactPage->buildLinks();
       echo $contactPage->buildBody();
       echo $contactPage->buildFooter();
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    ?>

    As demonstrated above, that's all the PHP code required to build a basic "Contact" web page, thanks to the capacity provided by inheritance. Please feel free to modify all the sample classes shown here, so you can develop your own examples for testing, and eventually acquire a better grounding in how to work with inherited classes in PHP 5.

    Final thoughts

    Sadly, this is the end of the series. As you have learned, inheritance is indeed a very powerful concept that allows, among other neat things, the development of object-oriented web sites. Of course, for larger applications the approach shown here isn't very suitable, but for small ones, the implementation of this technique, along with a simple template system, might be a pretty efficient solution.

    See you in the next PHP tutorial!


    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.

       · Over the course of this final article of the series, an entire, fictional web site ...
       · disappointing. sorry.I see no need to derive a new class for every page. Should...
       · Thank you for your comments on my PHP article. I found your suggestions very...
     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway