PHP
  Home arrow PHP arrow Page 3 - User-defined interfaces in PHP 5: Implementing (X)HTML Widgets
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

User-defined interfaces in PHP 5: Implementing (X)HTML Widgets
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek