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

Working with interface implementers: defining (X)HTML widget classes - 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

As I explained previously, (X)HTML widget classes present a very simple (but  powerful) structure. Since they implement the "HTMLRenderer" interface, all of them must provide a specific definition for the "toHTML()" method, in order to fit the requirements of interfaces, as I explained in the first article.

Given that, I'll define the first widget class, called "Table", which as the name implies, generates a regular HTML table:

// class Table
class Table implements HTMLRenderer{
    private $output='<table ';
    private $data=array();
    private $attributes=array();
    public function __construct($attributes=array()){
        if(count($attributes)<1){
            throw new Exception('Invalid number of attributes');
        }
        $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.='<tr><td>'.$data.'</td></tr>';
        }
        $this->output.='</table>';
        return $this->output;
    }
}

With the first widget class defined, let's dissect its code to understand each pertinent method.

As you can see through its definition, the "Table" class implements the "HTMLRenderer" interface. This is done by simply specifying the "interface" keyword after the class name, thus it's not a difficult thing. Then, the constructor accepts an "attributes" array parameter, which contains the proper attributes associated with a regular HTML table, for assignment as a class property.

The next method to be analyzed is "setData()", which is the unique modifier exposed by the class that provides a single access point for populating the table with either static or dynamic data. Notice that it accepts an array as incoming data and sets it up as another class property. Because of its simplicity, I'll deliberately avoid a more detailed explanation.

Now, turn your attention to the "toHTML()" method, since it implements an effective logic and introduces the use of the "instance of" operator. As you can see, basically the method performs two loops. The first one iterates over the table's attributes and builds the opening <table> tag together with its attributes.

The second loop is the most interesting though. Notice the checking process performed on the "data" array through the "instance of" operator. What this operator does is check whether the current array element is an implementer of the "HTMLRenderer" interface. If this is true, then the element is an object that also presents the "toHTML()" method, so it's called accordingly. The lines below  illustrate this concept:

foreach($this->data as $data){
    $data=($data instanceof HTMLRenderer)?$data->toHTML():$data;
    $this->output.='<tr><td>'.$data.'</td></tr>';
}

If you study the expression above, it should be clear that the recursive implementation of objects using the "HTMLRenderer" interface allows you to easily generate nested HTML elements. To clarify things, say that an array of paragraph objects is passed to the class. Since they also have a "toHTML()"method, as a result they will be rendered inside of each table row. Isn't that simple and powerful?

Now that you've hopefully understood the advantages of applying recursive interface implementers, I'll move forward to review the rest of (X)HTML widget classes.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

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

 



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

Dev Shed Tutorial Topics: