PHP
  Home arrow PHP arrow Page 4 - Building Dynamic Web Pages with Polymo...
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 
Moblin 
JMSL Numerical Library 
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 Dynamic Web Pages with Polymorphism in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    2007-03-28

    Table of Contents:
  • Building Dynamic Web Pages with Polymorphism in PHP 5
  • Building web pages using Polymorphism
  • Extending the concept of polymorphic classes
  • Seeing some polymorphic objects in action

  • 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 Dynamic Web Pages with Polymorphism in PHP 5 - Seeing some polymorphic objects in action


    (Page 4 of 4 )

    As I stated in the section that you just read, at this moment I'd like to create an educational example where all the classes that were defined previously are put to work in conjunction. As you'll see in the next few lines, the purpose of developing this example is to demonstrate how a small set of polymorphic objects can be used to create a complete web document.

    But, I don't want to bore you with irrelevant details any longer, therefore pay attention to the following code sample, please:  

    try{
       // build array of web page objects
      $elements=array(new H1('This is an H1 header','',''),new H2
    ('This is an H2 header','',''),new H3('This is an H3
    header','',''),new Paragraph('This is a
    paragraph','parid','parclass'),new Div('This is a
    DIV','divid','divclass'),new Link('This is a
    link','linkid','linkclass','default,htm'));
       // display web page elements using polymorphism
       foreach($elements as $element){
         echo $element->display();
       }
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    Even when the signature of the above script is short, it demonstrates in a nutshell how polymorphism can be used to generate a simple web page. Our page includes three headers and one paragraph, one containing DIV and finally a regular link.

    Nonetheless, the most interesting detail to notice here is how the same "display()" method is called by the different web page objects within a "foreach" construct to build the web document in question. Precisely, this condition is demonstrated by the following code block:

    foreach($elements as $element){
      echo $element->display();
    }

    All right, now that you have learned how to take advantage of polymorphism, I suggest you start using this fundamental pillar of object-oriented programming when you build your own PHP applications.

    Final thoughts

    Sad but true, we've come to the end of this tutorial. In this second installment of the series, I provided you with a clear example of how to use polymorphic objects to build dynamic web documents.

    However, if you think this educational journey has finished, I'm afraid you're wrong. There's still one last article to read, where I'll explain how to utilize the benefits of polymorphism to validate user-supplied data. You've been warned, so don't 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.

       · As you learned in the first article of the series, the implementation of polymorphic...
       · Alejandro, this article is Great !
       · Thank you for the compliments on my PHP article. I hope it's been useful to you and...
       · Thanks for your Great Work.This is very useful to New Php...
       · Thank you for commenting on my PHP article, and I'm glad to know it's been useful to...
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





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