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

More web page objects ahead: defining the remaining (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

As I said before, there are still a few more (X)HTML classes that need to be defined, in order to get all the web page objects complete. That said, here’s the signature of the remaining (X)HTML widgets:

// class Paragraph
class Paragraph extends HTMLElement{
    private $output='<p ';
    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.='</p>';
        return $this->output;
    }
}
// class HTMLlist
class HTMLlist extends HTMLElement{
    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('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.='<li>'.$data.'</li>';
        }
        $this->output.='</ul>';
        return $this->output;
    }
}
// class Form
class Form extends HTMLElement{
    private $output='<form ';
    private $data;
    private $fields=array();
    private $attributes=array();
    public function __construct($attributes=array()){
        $this->attributes=$attributes;
    }
    // set form data
    public function setData($data){
        if(!$data){
            throw new Exception('Data must not be empty');
        }
        $this->data=$data;
    }
    // add <input> element
    public function addInputField($fieldAttributes=array
(),$inputPart=''){
        $fieldOutput=$inputPart.'<input ';
        foreach($fieldAttributes as $attribute=>$value){
            $fieldOutput.=$attribute.'="'.$value.'" ';
        }
        $this->fields[]=$fieldOutput.'/>';
    }
    // add <textarea> element
    public function addTextArea($fieldAttributes=array
(),$inputPart=''){
        $fieldOutput=$inputPart.'<textarea ';
        foreach($fieldAttributes as $attribute=>$value){
            $fieldOutput.=$attribute.'="'.$value.'" ';
        }
        $fieldOutput=substr_replace($fieldOutput,'>',-1);
        $this->fields[]=$fieldOutput.'</textarea>';
    }
    // add <select> element
    public function addSelectField($fieldAttributes=array
(),$options=array(),$inputPart=''){
        $fieldOutput=$inputPart.'<select ';
        foreach($fieldAttributes as $attribute=>$value){
            $fieldOutput.=$attribute.'="'.$value.'" ';
        }
        $fieldOutput=substr_replace($fieldOutput,'>',-1);
        foreach($options as $option=>$value){
            $fieldOutput.='<option
value="'.$value.'">'.$option.'</option>';
        }
        $this->fields[]=$fieldOutput.'</select>';
    }
    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;
        foreach($this->fields as $field){
            $this->output.=$field;
        }
        $this->output.='</form>';
        return $this->output;
    }
}

Fine, I think these are all the (X)HTML widget classes required for constructing a decent mechanism, which uses PHP objects for creating web pages. However, so far I didn’t mention or even code a HTTP compression class anywhere, thus…how does data compression fit into the picture? In response to that, what I’ll do next is define a couple of additional classes that integrate all the prior (X)HTML widgets and apply HTTP compression to dynamically-generated web documents. To learn how this is achieved, please read the following section.



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