PHP
  Home arrow PHP arrow Page 4 - 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 - A final example of recursion: creating a template processor class


    (Page 4 of 4 )

    The final hands-on example of this article consists of developing a template processor class in PHP 5, which among other things has the capacity to recursively parse template files by replacing nested placeholders with data coming from different sources.

    To begin with, this is how a typical template file looks, before being parsed by the respective class:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>{title}</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-
    8859-1" />
    <link rel="stylesheet" type="text/css" href="mystyles.css" />
    </head>
    <body>
    <div id="header">{header}</div>
    <div id="navbar">{navbar {subnavigationbar1}{ subnavigationbar2}}</div>
    <div id="leftcol">{leftcontent}</div>
    <div id="content">{maincontent}</div>
    <div id="rightcol">{rightcontent}</div>
    <div id="footer">{footer}</div>
    </body>
    </html>

    As you can see, the template file that I just created has some nested placeholders that require the use of a recursive method to replace them with actual data. That’s precisely the function of the “TemplateProcessor” class that I’ve listed below:

    class TemplateProcessor {
        private $output='';// set default value for general class
    output
        private $rowTag='p';// set default value for database row tag
        private $tags=array();// set default value for tags
        private $templateFile='default_template.htm';// set default
    value for template file
        private $cacheFile='default_cache.txt';// set default value
    for cache file
        private $expiry=3600;// set default value for cache
    expiration
        public function __construct($tags=array()){
            if(count($tags)<1){
                throw new Exception('Invalid number of tags');
            }
            if($this->isCacheValid()){
                // read data from cache file
                $this->output=$this->readCache();
            }
            else{
                $this->tags=$tags;
                // read template file
                $this->output=file_get_contents($this->templateFile);
                // process template file
                $this->processTemplate($this->tags);
                // clean up empty tags
                $this->output=preg_replace("/{w}|}/",'',$this-
    >output);
                // write compressed data to cache file
                $this->writeCache();
            }
            // send gzip encoding http header
            $this->sendEncodingHeader();
        }
        // check cache validity
        private function isCacheValid(){
            // determine if cache file is valid or not
            if(file_exists($this->cacheFile)&&filemtime($this-
    >cacheFile)>(time()-$this->expiry)){
                return true;
            }
            return false;
        }
        // process template file
        private function processTemplate($tags){
            foreach($tags as $tag=>$data){
                // if data is array, traverse recursive array of tags
                if(is_array($data)){
                    $this->output=preg_replace("/{$tag/",'',$this-
    >output);
                    $this->processTemplate($data);
                }
                // if data is a file, fetch processed file
                elseif(file_exists($data)){
                    $data=$this->processFile($data);
                }
                // if data is a MySQL result set, obtain a formatted
    list of database rows
                elseif(@get_resource_type($data)=='mysql result'){
                    $rows='';
                    while($row=mysql_fetch_row($data)){
                        $cols='';
                        foreach($row as $col){
                            $cols.='&nbsp;'.$col.'&nbsp;';
                        }
                        $rows.='<'.$this-
    >rowTag.'>'.$cols.'</'.$this->rowTag.'>';
                    }
                    $data=$rows;
                }
                // if data contains the '[code]' elimiter, parse data
    as PHP code
                elseif(substr($data,0,6)=='[code]'){
                    $data=eval(substr($data,6));
                }
                $this->output=str_replace('{'.$tag.'}',$data,$this-
    >output);
            }
        }
        // process input file
        private function processFile($file){
              ob_start();
              include($file);
              $contents=ob_get_contents();
              ob_end_clean();
              return $contents;
        }
        // write compressed data to cache file
        private function writeCache(){
            if(!$fp=fopen($this->cacheFile,'w')){
                throw new Exception('Error writing data to cache
    file');
            }
            fwrite($fp,$this->getCompressedHTML());
            fclose($fp);
        }
        // read compressed data from cache file
        private function readCache(){
            if(!$cacheContents=file_get_contents($this->cacheFile)){
                throw new Exception('Error reading data from cache
    file');
            }
            return $cacheContents;
        }
        // return overall output
        public function getHTML(){
              return $this->output;
        }
        // return compressed output
        private function getCompressedHTML(){
            // check if browser supports gzip encoding
            if(strstr($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')){
                // start output buffer
                ob_start();
                // echo page contents to output buffer
                echo $this->output;
                // crunch (X)HTML content & compress it with gzip
                $this->output=gzencode(preg_replace("/
    (rn|n)/","",ob_get_contents()),9);
                // clean up output buffer
                ob_end_clean();
                // return compressed (X)HTML content
                return $this->output;
            }
            return false;
        }
        // send gzip encoding http header
        public function sendEncodingHeader(){
            header('Content-Encoding: gzip');
        }
    }

    As you can see, aside from using recursion for parsing template files, the above class is also capable of caching and compressing the (X)HTML output of parsed web pages. This makes it a quite useful piece of code, particularly if you work very often with template systems. But now, turn your attention to the following class method:

    private function processTemplate($tags){
        foreach($tags as $tag=>$data){
            // if data is array, traverse recursive array of tags
            if(is_array($data)){
                $this->output=preg_replace("/{$tag/",'',$this-
    >output);
                $this->processTemplate($data);
            }
            // if data is a file, fetch processed file
            elseif(file_exists($data)){
                $data=$this->processFile($data);
            }
            // if data is a MySQL result set, obtain a formatted list
    of database rows
            elseif(@get_resource_type($data)=='mysql result'){
                $rows='';
                while($row=mysql_fetch_row($data)){
                    $cols='';
                    foreach($row as $col){
                        $cols.='&nbsp;'.$col.'&nbsp;';
                    }
                    $rows.='<'.$this->rowTag.'>'.$cols.'</'.$this-
    >rowTag.'>';
                }
                $data=$rows;
            }
            // if data contains the '[code]' elimiter, parse data as
    PHP code
            elseif(substr($data,0,6)=='[code]'){
                $data=eval(substr($data,6));
            }
            $this->output=str_replace('{'.$tag.'}',$data,$this-
    >output);
        }
    }

    Regarding the above method, you can see that it calls itself (in other words, uses recursion), in order to replace all the nested placeholders that eventually might be included inside the corresponding template file. Additionally, the method is capable of processing MySQL result sets, parsing dynamic PHP files and evaluating input strings as PHP code.

    Now, pay attention to the example below, which illustrates how to use the recursive capabilities of the “TemplateProcessor” class:

    try{
        // define input tags for template processor class
        $tags=array('title'=>'PHP 5 Template Processor','header'=>'This is the header section','navbar'=>array
    ('subnavigationbar1'=>'subnavbar1.php','navigationbar2'=>
    'subnavbar2.php'),'
    leftcontent'=>'leftcontent','maincontent'=>'This is the main
    section','rightcontent'=>'right content','footer'=>'This is the
    footer section');
        // instantiate a new template processor object
        $tpl=new TemplateProcessor($tags);
        // display compressed page
        echo $tpl->getHTML();
    }
    catch(Exception $e){
        echo $e->getMessage();
        exit();
    }

    In this case, I defined a recursive array of input tags, to be passed directly to the constructor of the “TemplateProcessor” class. Since the template file you saw previously contains some nested placeholders, I specified the elements corresponding to the "navbar" key to also be arrays (hence the concept of recursive array), thus the “subnavigationbar1” and “subnavigationbar2” placeholders will be replaced with the parsed output of the “subnavbar1.php” and “subnavbar2.php” PHP files respectively.

    Considering that these dynamic files might be created as follows:

    <?php
    // definition for 'subnavbar1.php' file
    echo 'This navigation section was generated at '.date('H:i:s');
    ?>

    <?php
    // definition for 'subnavbar2.php' file
    echo 'This navigation section was generated at '.date('H:i:s');
    ?>

    The output of the previous script would look like this:

    This is the header section

    This navigation section was generated at 12:45:03
    This navigation section was generated at 12:45:03
    left content
    This is the main section

    right content

    This is the footer section

    As you can see, the recursive “processTemplate” method has iterated over the array of input tags and replaced all the placeholders with the respective data, which demonstrates the benefits of having at your disposal a method that uses recursion for parsing template files.

    Final thoughts

    In this tutorial you learned how to define recursive methods in PHP when developing object-oriented applications. I showed you two specific examples: a web page generator class and a template processor. In both cases you saw how recursion can be quite useful for performing a deep scan on recursive arrays.

    However, are you thinking this is it about recursion? You’re wrong. Over the last part of the series I’ll explain how to develop a MySQL-driven discussion forum, which uses recursive methods for fetching user messages from a database table. See you in the next part!


    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.

       · 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 1 hosted by Hostway
    Stay green...Green IT