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

Using Recursive Methods in Object-based PHP Applications
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    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:
      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


    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
     

       

    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 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek