PHP
  Home arrow PHP arrow Page 4 - User-defined Interfaces in PHP 5: Building a Page Generator
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: Building a Page Generator
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2006-01-09


    Table of Contents:
  • User-defined Interfaces in PHP 5: Building a Page Generator
  • Working with multiple interface implementers: defining a page generator class
  • Building object-based web pages: implementing the “PageGenerator” class
  • Full source code: listing the complete classes

  • 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: Building a Page Generator - Full source code: listing the complete classes
    ( Page 4 of 4 )

    For the sake of completeness, here is the full list of all the classes developed during the series. I hope you enjoy studying them and introducing your own modifications. See you soon!

    // interface HTMLRenderer
    // defines generic method toHTML()
    interface HTMLRenderer {
        public abstract function toHTML();
    }
    // class MySQL
    class MySQL{
        private $conId; // connection identifier
        private $host; // MySQL host
        private $user; // MySQL username
        private $password; // MySQL password
        private $database; // MySQL database
        // constructor
        public function __construct($options=array()){
            // validate incoming parameters
            if(count($options)<4){
                throw new Exception('Invalid number of connection
    parameters');
            }
            foreach($options as $parameter=>$value){
                if(!$parameter||!$value){
                    throw new Exception('Invalid connection
    parameters');
                }
                $this->{$parameter}=$value;
            }
            // connect to MySQL
     $this->connectDB();
            }
            // connect to MYSQL server and select database
            private function connectDB(){
                if(!$this->conId=mysql_connect($this->host,$this-
    >user,$this->password)){
                     throw new Exception('Error connecting to the server');
     }
     if(!mysql_select_db($this->database,$this->conId)){
         throw new Exception('Error selecting database');
     }
            }
            // perform query
            public function query($query){
     if(!$this->result=mysql_query($query,$this->conId)){
         throw new Exception('Error performing query '.$query);
     }
     // return new Result object
     return new Result($this,$this->result);
           }
    }
    //class Result
    class Result implements HTMLRenderer{
        private $output; // dynamic output
        private $mysql; // instance of MySQL object
        private $result; // MySQL result set
        // constructor
        public function __construct($mysql,$result){
            $this->mysql=$mysql;
            $this->result=$result;
        }
        // fetch row
        public function fetchRow(){
            return mysql_fetch_array($this->result,MYSQL_ASSOC);
        }
        // count rows
        public function countRows(){
            if(!$rows=mysql_num_rows($this->result)){
                throw new Exception('Error counting rows');
            }
            return $rows;
        }
        // count affected rows
        public function countAffectedRows(){
            if(!$rows=mysql_affected_rows($this->mysql->conId)){
     throw new Exception('Error counting affected rows');
            }
            return $rows;
        }
        // get ID from last inserted row
        public function getInsertID(){
            if(!$id=mysql_insert_id($this->mysql->conId)){
     throw new Exception('Error getting ID');
            }
            return $id;
        }
        // seek row
        public function seekRow($row=0){
            if(!mysql_data_seek($this->result,$row)){
     throw new Exception('Error seeking data');
            }
        }
        // return HTML rendered result set
        public function toHTML(){
            $this->output='<table>';
            while($row=$this->fetchRow()){
                $this->output.='<tr>';
                foreach($row as $field=>$value){
                    $this->output.='<td>'.$value.'</td>';
                }
                $this->output.='</tr>';
            }
            $this->output.='</table>';
            return $this->output;
        }
    }
    // ************************************
    // begin of core class definitions
    // ************************************
    // class ObjectFactory - returns HTML widget objects
    abstract class ObjectFactory{
        private function __construct(){}
        public function createObject($type,$attributes=array()){
            if(!class_exists($type)||!is_array($attributes)){
                throw new Exception('Invalid object parameters');
            }
            return new $type($attributes);
        }
    }
    // class PageGenerator - creates web pages
    class PageGenerator{
        private $output;
        private $title;
        private $keywords;
        private $cssFile;
        private $jsFile;
        public function __construct(){
            $this->output='';
            $this->title='Sample Page';
            $this->keywords='PHP,Interfaces,OOP,Type,Hinting';
            $this->cssFile='styles.css';
            $this->jsFile='global_functions.js';
        }
        public function setTitle($title){
            $this->title=$title;
        }
        public function setKeywords($keywords){
            $this->keywords=$keywords;
        }
        public function setCssFile($cssFile){
            $this->cssFile=$cssFile;
        }
        public function setJsFile($jsFile){
            $this->jsFile=$jsFile;
        }
        public function makeHeader(){
            $this->output='<html><head><title>'.$this-
    >title.'</title><meta name="keywords" content="'.$this-
    >keywords.'" /><link rel="stylesheet" href="'.$this->cssFile.'"
    type="text/css"><script language="javascript" src="'.$this-
    >jsFile.'"></script></head><body>';
        }
        public function addHTMLRenderableObject(HTMLRenderer $object){
            $this->output.=$object->toHTML();
        }
        public function makeFooter(){
            $this->output.='</body></html>';
        }
        public function getHTML(){
            return $this->output;
        }
    }
    // *********************************************
    // end of core class definitions
    // *********************************************
    // *********************************************
    // begin of HTML widget class definitions
    // *********************************************
    // 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;
        }
    }
    // 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;
        }
    }
    // class Header1
    class Header1 implements HTMLRenderer{
        private $output='<h1 ';
        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.='</h1>';
            return $this->output;
        }
    }
    // class Header2
    class Header2 implements HTMLRenderer{
        private $output='<h2 ';
        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.='</h2>';
            return $this->output;
        }
    }
    // class Header3
    class Header3 implements HTMLRenderer{
        private $output='<h3 ';
        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.='</h3>';
            return $this->output;
        }
    }
    // class Header4
    class Header4 implements HTMLRenderer{
        private $output='<h4 ';
        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.='</h4>';
            return $this->output;
        }
    }
    // class Header5
    class Header5 implements HTMLRenderer{
        private $output='<h5 ';
        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.='</h5>';
            return $this->output;
        }
    }
    // class Header6
    class Header6 implements HTMLRenderer{
        private $output='<h6 ';
        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.='</h6>';
            return $this->output;
        }
    }
    // 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;
        }
    }
    // class Form
    class Form implements HTMLRenderer{
        private $output='<form ';
        private $data;
        private $fields=array();
        private $attributes=array();
        public function __construct($attributes){
            $this->attributes=$attributes;
        }
        // set generic form data
        public function setData($data){
            $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 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;
            foreach($this->fields as $field){
                $this->output.=$field;
            }
            $this->output.='</form>';
            return $this->output;
        }
    }
    // *******************************************
    // end of HTML widget class definitions
    // *******************************************

    Note: all the classes have been tested on Apache 2.0.48 and PHP 5.0.4.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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