Home arrow PHP arrow Page 3 - Using Recursive Methods in Object-based PHP Applications

Defining a recursive method: creating a web page generator class - PHP

Welcome to the second tutorial of the series “Recursion in PHP.” Comprised of three parts, this series introduces the fundamentals of recursion in PHP, including the definition and use of recursive functions in procedural PHP scripts, as well as the creation of recursive methods in object-oriented Web applications.

TABLE OF CONTENTS:
  1. Using Recursive Methods in Object-based PHP Applications
  2. Applying recursion in object-oriented programming: creating object-based web page elements
  3. Defining a recursive method: creating a web page generator class
  4. A final example of recursion: creating a template processor class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
May 08, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap

Dev Shed Tutorial Topics: