PHP
  Home arrow PHP arrow Page 4 - 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? 
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 - Object-based (X)HTML rendering: more (X)HTML widgets to define
    ( Page 4 of 5 )

    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
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT