HomePHP 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.
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.