PHP
  Home arrow PHP arrow Page 2 - 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 - Applying recursion in object-oriented programming: creating object-based web page elements


    (Page 2 of 4 )

    In order to demonstrate how to write recursive methods within an object-oriented application, what I’ll do first is define a set of simple PHP 5 (X)HTML widget classes, geared to creating dynamic web pages via an object-based approach. At a later time, these classes will be integrated within a single page generator class, which will expose a recursive method for programmatically rendering web documents.

    So, I’ll start listing some of these PHP 5 (X)HTML widget classes, in conjunction with the abstract base class, which all the classes are derived from:

    // define abstract class HTMLElement
    abstract class HTMLElement{
        protected $attributes;
        protected function __construct($attributes){
            if(!is_array($attributes)){
                throw new Exception('Invalid attribute type');
            }
            $this->attributes=$attributes;
        }
        // abstract 'getHTML()' method
        abstract protected function getHTML();
    }
    // define 'Div' class
    class Div extends HTMLElement{
        private $output='<div ';
        private $data;
        public function __construct($attributes=array(),$data){
            if(!$data instanceof HTMLElement&&!is_string($data)){
                throw new Exception('Invalid parameter type');
            }
            parent::__construct($attributes);
            $this->data=$data;
        }
        // concrete implementation for 'getHTML()' method
        public function getHTML(){
            foreach($this->attributes as $attribute=>$value){
                $this->output.=$attribute.'="'.$value.'" ';
            }
            $this->output=substr_replace($this->output,'>',-1);
            $this->output.=($this->data instanceof HTMLElement)?
    $this->data->getHTML():$this->data;
            $this->output.='</div>';
            return $this->output;
        }
    }
    // define 'Header1' class
    class Header1 extends HTMLElement{
        private $output='<h1 ';
        private $data;
        public function __construct($attributes=array(),$data){
            if(!$data instanceof HTMLElement&&!is_string($data)){
                throw new Exception('Invalid parameter type');
            }
            parent::__construct($attributes);
            $this->data=$data;
        }
        // concrete implementation for 'getHTML()' method
        public function getHTML(){
            foreach($this->attributes as $attribute=>$value){
                $this->output.=$attribute.'="'.$value.'" ';
            }
            $this->output=substr_replace($this->output,'>',-1);
            $this->output.=($this->data instanceof HTMLElement)?
    $this->data->getHTML():$this->data;
            $this->output.='</h1>';
            return $this->output;
        }
    }
    // define 'Paragraph' class
    class Paragraph extends HTMLElement{
        private $output='<p ';
        private $data;
        public function __construct($attributes=array(),$data){
            if(!$data instanceof HTMLElement&&!is_string($data)){
                throw new Exception('Invalid parameter type');
            }
            parent::__construct($attributes);
            $this->data=$data;
        }
        // concrete implementation for 'getHTML()' method
        public function getHTML(){
            foreach($this->attributes as $attribute=>$value){
                $this->output.=$attribute.'="'.$value.'" ';
            }
            $this->output=substr_replace($this->output,'>',-1);
            $this->output.=($this->data instanceof HTMLElement)?
    $this->data->getHTML():$this->data;
            $this->output.='</p>';
            return $this->output;
        }
    }
    // define 'UnorderedList' class
    class UnorderedList extends HTMLElement{
        private $output='<ul ';
        private $items=array();
        public function __construct($attributes=array(),$items=array
    ()){
            parent::__construct($attributes);
            if(!is_array($items)){
                throw new Exception('Invalid parameter for list
    items');
            }
            $this->items=$items;
        }
        // concrete implementation for 'getHTML()' method
        public function getHTML(){
            foreach($this->attributes as $attribute=>$value){
                $this->output.=$attribute.'="'.$value.'" ';
            }
            $this->output=substr_replace($this->output,'>',-1);
            foreach($this->items as $item){
                $this->output.=($item instanceof
    HTMLElement)?'<li>'.$item->getHTML ().'</li>':'<li>'.$item.'</li>';
            }
            $this->output.='</ul>';
            return $this->output;
        }
    }

    Right, at this point I listed all the (X)HTML widget classes required for constructing a basic web page. Of course, if you’ve been reading some of my previous PHP articles, then these classes should already be familiar to you, since I’ve used them as part of some previous code samples.

    Now that you know how the (X)HTML widgets are defined, I’ll create a web page generator class that uses recursion for constructing the corresponding web documents. To see how this class will be defined, please go ahead and read the next section.

    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