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

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

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: