PHP
  Home arrow PHP arrow Page 3 - Using Recursive Methods in Object-base...
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

Using Recursive Methods in Object-based PHP Applications
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2006-05-08

    Table of Contents:
  • Using Recursive Methods in Object-based PHP Applications
  • Applying recursion in object-oriented programming: creating object-based web page elements
  • Defining a recursive method: creating a web page generator class
  • A final example of recursion: creating a template processor class

  • 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


    Using Recursive Methods in Object-based PHP Applications - Defining a recursive method: creating a web page generator class


    (Page 3 of 4 )

    Having shown the previous (X)HTML widget classes, the web page generator that I plan to build consists essentially of a single PHP 5 class, which takes up a recursive array of web page objects and renders the corresponding web document. This is how the “PageGenerator” class looks:

    class PageGenerator{
        private $output='';
        private $title;
        public function __construct($title='Default Page'){
            $this->title=$title;
        }
        public function doHeader(){
            $this->output='<html><head><title>'.$this-
    >title.'</title></head><body>';
        }
        public function addHTMLElements($htmlElements){
            foreach($htmlElements as $htmlElement){
                if(is_array($htmlElement)){
                    $this->addHTMLElements($htmlElement);
                }
                else{
                    $this->output.=$htmlElement->getHTML();
                }
            }
        }
        public function doFooter(){
            $this->output.='</body></html>';
        }
        public function fetchHTML(){
            return $this->output;
        }
    }

    As you can see, my “PageGenerator” class is very easy to understand. I deliberately coded most of its methods in a simple way, so you can pay attention to the core engine of the class, the public “addHTMLElement()” method. Notice how this method accepts a recursive array of (X)HTML widget objects and iterates over them, in order to construct the corresponding web page.

    With reference to making the method recur, the following code block:

    if(is_array($htmlElement)){
        $this->addHTMLElements($htmlElement);
    }

    demonstrates how the method calls itself, in order to traverse deeply the array of (X)HTML widget objects and, in turn, display its markup code. Due to the simplicity of the remaining methods of the web page generator class, I won’t explain how they work; instead, I’ll show you an example that illustrates how to utilize the “PageGenerator” class. Here is the pertinent sample code:

    // spawn some HTML elements
    $h1=new Header1(array
    ('name'=>'header1','class'=>'headerclass'),'Content for H1
    element goes here');
    $div=new Div(array('name'=>'div1','class'=>'divclass'),'Content
    for Div element goes here');
    $par=new Paragraph(array
    ('name'=>'par1','class'=>'parclass'),'Content for Paragraph
    element goes here');
    $ul=new UnorderedList(array
    ('name'=>'list1','class'=>'listclass'),array
    ('item1'=>'value1','item2'=>'value2','item3'=>'value3'));
    // make recursive array with HTML objects
    $htmlElements=array($h1,array($div,$par,$ul));
    // instantiate 'PageGenerator' object
    $pageGen=new PageGenerator();
    // generate web page
    $pageGen->doHeader();
    $pageGen->addHTMLElements($htmlElements);
    $pageGen->doFooter();
    // display web page
    echo $pageGen->fetchHTML();

    As shown in the above example, first I spawned some (X)HTML widget objects, including a header, a DIV, a paragraph, and finally an unordered list. Then I proceeded to build a recursive array with them. This array was passed directly to the “addHTML()” method of the page generator class, in order to fetch the corresponding objects and display the web page in question.

    In this case, of course I’m not asking you to pay attention to how to create object-based web pages, since it’s a straightforward process that I covered in previous tutorials. Instead, what I’d like to emphasize here is the ability of the “PageGenerator” class to process (X)HTML widgets, whether they’re arranged as a recursive array or not.

    Okay, by the example you saw before, hopefully you learned how to define recursive methods inside PHP classes, which is certainly a process that doesn’t differ too much from creating recursive functions. However, there’s still an additional example I want you to see. In the next section, I’ll show you the signature of a template processor class, which also uses recursive logic for parsing template files.

    Please keep reading to find out how this class will be created.

    More PHP Articles
    More By Alejandro Gervasio


       · Over the course of this second article, you'll learn how to define recursive methods...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - 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
    - 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
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




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