PHP
  Home arrow PHP arrow Page 4 - Building Dynamic Web Pages with Polymorphism 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 Dynamic Web Pages with Polymorphism in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 13
    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:
      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 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!



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