PHP
  Home arrow PHP arrow Page 3 - User-defined interfaces in PHP 5: Impl...
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

User-defined interfaces in PHP 5: Implementing (X)HTML Widgets
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2005-12-26

    Table of Contents:
  • User-defined interfaces in PHP 5: Implementing (X)HTML Widgets
  • Generic (X)HTML generation: defining the "HTMLRenderer" interface
  • Working with interface implementers: defining (X)HTML widget classes
  • Object-based (X)HTML rendering: more (X)HTML widgets to define
  • Completing the list of (X)HTML widgets: defining classes for rendering headers and forms

  • 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


    User-defined interfaces in PHP 5: Implementing (X)HTML Widgets - Working with interface implementers: defining (X)HTML widget classes


    (Page 3 of 5 )

    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


       · The second article illustrates the use of interfaces by creating a set of (X)HTML...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - 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
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT