Home arrow PHP arrow Page 2 - Building Object-oriented Web Pages with HTTP Compression in PHP

Object-based web pages in a nutshell: defining (X)HTML widget classes - PHP

Here we go! Welcome to the third -– and last -- part of the series “Using HTTP compression in PHP.” Just in case you didn’t know, this set of tutorials explores the advantages of using HTTP compression on dynamic PHP pages, reducing their download times and increasing the overall performance of PHP applications.

TABLE OF CONTENTS:
  1. Building Object-oriented Web Pages with HTTP Compression in PHP
  2. Object-based web pages in a nutshell: defining (X)HTML widget classes
  3. More web page objects ahead: defining the remaining (X)HTML widget classes
  4. Putting the pieces together: compressing the output of a web page generator class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
April 24, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

To demonstrate how HTTP compression can be appropriately used on web pages that are created by PHP objects, I’ll begin by showing you several PHP 5-based (X)HTML widget classes, which are the building blocks of object-based web documents. Probably, and provided that you read some of my previous PHP articles, the definition of these classes will be already familiar to you, since I used them in several code samples.

As you’ll see, all the (X)HTML classes are created as subclasses of a generic abstract “HTMLElement” class, so have a look at them, in addition to this base class I mentioned before:

// abstract 'HTMLElement' class
abstract class HTMLElement {
    protected abstract function getHTML();
}

// 'Table' class
class Table extends HTMLElement{
    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('Data must not be empty');
        }
        $this->data=$data;
    }
    public function getHTML(){
        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 HTMLElement)?$data->getHTML
():$data;
            $this->output.='<tr><td>'.$data.'</td></tr>';
        }
        $this->output.='</table>';
        return $this->output;
    }
}

// class Div
class Div extends HTMLElement{
    private $output='<div ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        if(!$data){
            throw new Exception('Data must not be empty');
        }
        $this->data=$data;
    }
    public function getHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLElement)?
$this->data->getHTML():$this->data;
        $this->output.='</div>';
        return $this->output;
    }
}

// class Header1
class Header1 extends HTMLElement{
    private $output='<h1 ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        if(!$data){
            throw new Exception('Data must not be empty');
        }
        $this->data=$data;
    }
    public function getHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLElement)?
$this->data->getHTML():$this->data;
        $this->output.='</h1>';
        return $this->output;
    }
}

// class Header2
class Header2 extends HTMLElement{
    private $output='<h2 ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        if(!$data){
            throw new Exception('Data must not be empty');
        }
        $this->data=$data;
    }
    public function getHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLElement)?
$this->data->getHTML():$this->data;
        $this->output.='</h2>';
        return $this->output;
    }
}

// class Header3
class Header3 extends HTMLElement{
    private $output='<h3 ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        if(!$data){
            throw new Exception('Data must not be empty');
        }
        $this->data=$data;
    }
    public function getHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLElement)?
$this->data->getHTML():$this->data;
        $this->output.='</h3>';
        return $this->output;
    }
}

// class Header4
class Header4 extends HTMLElement{
    private $output='<h4 ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        if(!$data){
            throw new Exception('Data must not be empty');
        }
        $this->data=$data;
    }
    public function getHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLElement)?
$this->data->getHTML():$this->data;
        $this->output.='</h4>';
        return $this->output;
    }
}

// class Header5
class Header5 extends HTMLElement{
    private $output='<h5 ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        if(!$data){
            throw new Exception('Data must not be empty');
        }
        $this->data=$data;
    }
    public function getHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLElement)?
$this->data->getHTML():$this->data;
        $this->output.='</h5>';
        return $this->output;
    }
}

// class Header6
class Header6 extends HTMLElement{
    private $output='<h6 ';
    private $data;
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    public function setData($data){
        if(!$data){
            throw new Exception('Data must not be empty');
        }
        $this->data=$data;
    }
    public function getHTML(){
        foreach($this->attributes as $attribute=>$value){
            $this->output.=$attribute.'="'.$value.'" ';
        }
        $this->output=substr_replace($this->output,'>',-1);
        $this->output.=($this->data instanceof HTMLElement)?
$this->data->getHTML():$this->data;
        $this->output.='</h6>';
        return $this->output;
    }
}

Right, as you can see, I listed above the first (X)HTML widget classes that will perform the creation of object-based web page elements (in this case they display tables, divs, and headers respectively). Now, getting rid of irrelevant details, such as explaining the underlying logic of (X)HTML widget classes, the only point worth noting above is the definition of the abstract “HTMLElement” class, located on top of the corresponding objects hierarchy.

The question is: why did I create this class? Well, luckily the answer is much simpler than you think. I simply defined it in order to group all the respective subclasses under the same object family, and provide each of them with the ability to render nesting page elements, in this case by applying a recursive implementation of their “getHTML()” method.

Now, hopefully the reason for defining this base abstract class should be clear to you; thus let’s jump together into the next section and continue defining the remaining (X)HTML widget classes.



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