Home arrow PHP arrow Page 4 - User-defined interfaces in PHP 5: Implementing (X)HTML Widgets

Object-based (X)HTML rendering: more (X)HTML widgets to define - PHP

In this second part of the series, you will learn the basics of object-oriented web page generation through the use of (X)HTML widgets. You will also see how objects implement the “HTMLRenderer” interface to explicitly define its functionality by using the “toHTML()” method.

TABLE OF CONTENTS:
  1. User-defined interfaces in PHP 5: Implementing (X)HTML Widgets
  2. Generic (X)HTML generation: defining the "HTMLRenderer" interface
  3. Working with interface implementers: defining (X)HTML widget classes
  4. Object-based (X)HTML rendering: more (X)HTML widgets to define
  5. Completing the list of (X)HTML widgets: defining classes for rendering headers and forms
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
December 26, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Once the first "Table" widget has been defined and studied in detail, explaining the logic of the remaining (X)HTML widgets is a fairly easy thing. As expected, divs, paragraphs, or lists implement the same "HTMLRenderer" interface, which directly implies the existence of a concrete definition for the "toHTML()" method. With this concept in mind, I'll continue defining a few more widget classes, starting with the "Div" class:

// class Div
class Div implements HTMLRenderer{
    private $output='<div ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        $this->data=$data;
    }
    public function toHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLRenderer)?$this->data->toHTML():$this->data;
        $this->output.='</div>';
        return $this->output;
    }
}

Closely similar to the "Table" class, the above "Div" class exposes the same methods for assigning class properties, in conjunction with the core "toHTML()" method. In this case, the method also defines a recursive implementation of the "HTMLRenderer" interface, so building nested elements is possible by checking the type of object passed through the "instance of" operator. As discussed before, the same logic is applied to each widget class, so understanding them is quite simple.

Now, let's define a few more (X)HTML widget classes, in this case for rendering paragraphs and HTML lists. Here are the corresponding classes:

// class Paragraph
class Paragraph implements HTMLRenderer{
    private $output='<p ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        $this->data=$data;
    }
    public function toHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLRenderer)?$this->data->toHTML():$this->data;
        $this->output.='</p>';
        return $this->output;
    }
}

// class HTMLlist
class HTMLlist implements HTMLRenderer{
    private $output='<ul ';
    private $data=array();
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data=array()){
        if(count($data)<1){
            throw new Exception('Empty data set');
        }
        $this->data=$data;
    }
    public function toHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        foreach($this->data as $data){
            $data=($data instanceof HTMLRenderer)?$data->toHTML():$data;
            $this->output.='<li>'.$data.'</li>';
        }
        $this->output.='</ul>';
        return $this->output;
    }
}

Certainly, the above defined classes looks very similar to the previous ones, thus they implement the same "HTMLRenderer" interface. Particularly, the first one takes care of rendering paragraphs, while the second one is tasked with displaying unordered lists. Again, I put strong emphasis on the specific implementation of the "toHTML()" method for both classes.

The next step consists of listing the remaining (X)HTML widgets, by defining the corresponding classes for displaying headers and forms.



 
 
>>> 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: