PHP
  Home arrow PHP arrow Page 2 - 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 - Working with multiple interface implementers: defining a page generator class
    ( Page 2 of 4 )

    In order to work with multiple implementers of the “HTMLRenderable” interface, what I’ll do next is define a “PageGenerator” class, which will simply be tasked with building a regular web page. Although the class structure is rather simple, it’s a useful example for showing how objects belonging to different types can be grouped together into the same class, and programmatically display a web page.

    First, let’s see how the class looks and then explain its underlying logic. Therefore, here is the definition for the “PageGenerator” class:

    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;
        }
    }

    Now that the above class has been defined, let’s study in detail each relevant method. First, the constructor performs the initialization of the corresponding class properties, by assigning default values to each of them. With reference to the data members exposed by the class, I’ve decided to provide common properties, such as title and keywords, in conjunction with CSS and JavaScript files. Of course, feel free to add your own improvements, so the class will eventually be more flexible and robust.

    The next methods to be reviewed are “makeHeader()” and “makeFooter()” respectively. Certainly they’re not difficult to understand at all, due mainly to the fact that they simply build the header and footer of the web page. The only thing worth noting here is the addition of the values assigned as class properties to the overall page’s output, during the generation of the header section. Also, the page footer is built by simply appending the closing </body></html> tags, as you probably saw many times.

    In addition to the above described methods, the class is provided with the required modifiers for each exposed property, thus assigning new values to a property is extremely easy.

    Now, let’s get rid of some rather irrelevant details of classic methods, and pay attention to the “addHTMLRenderableObject()” method, which does the hard work for rendering the web page. Its definition looks like this:

    public function addHTMLRenderableObject(HTMLRenderer $object){
        $this->output.=$object->toHTML();
    }

    Simple yet powerful, what this method does is accept as an argument, an object that implements the “HTMLRenderer” interface -- so, the referenced objects present a “toHTML()” method -- and append the object’s (X)HTML markup to the general output. The following line illustrates this concept:

    $this->output.=$object->toHTML();

    Through the use of the “type hinting” feature available in PHP5, I’m forcing the method to accept only objects of type “HTMLRenderer”, throwing a fatal error exception if the object passed in doesn’t fit the specified type. It’s important that you realize the advantages of having such a useful method, since it allows you to use any object that implements the “toHTML()” method to append some (X)HTML markup and display the page.

    To finish reviewing the class methods,  the last one, “getHTML()”, returns the complete page output for displaying on the browser:

    public function getHTML(){
        return $this->output;
    }

    Now that you know how the page generator class works, it’s time to see an example to demonstrate its use.



     
     
    >>> 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 2 hosted by Hostway
    Stay green...Green IT