Home arrow PHP arrow Page 2 - User-defined Interfaces in PHP 5: Building a Page Generator

Working with multiple interface implementers: defining a page generator class - PHP

In this final part of the series on user-defined interfaces in PHP 5, Alejandro puts all of the classes created in the previous articles together to work as a page generator. This allows complex page generation to be translated into a more flexible process through an object-based solution.

TABLE OF CONTENTS:
  1. User-defined Interfaces in PHP 5: Building a Page Generator
  2. Working with multiple interface implementers: defining a page generator class
  3. Building object-based web pages: implementing the “PageGenerator” class
  4. Full source code: listing the complete classes
By: Alejandro Gervasio
Rating: starstarstarstarstar / 12
January 09, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

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: