Home arrow PHP arrow Page 2 - User-defined interfaces in PHP 5: Implementing (X)HTML Widgets

Generic (X)HTML generation: defining the "HTMLRenderer" interface - PHP

In this second part of the series, you will learn the basics of object-oriented web page generation through the use of (X)HTML widgets. You will also see how objects implement the “HTMLRenderer” interface to explicitly define its functionality by using the “toHTML()” method.

  1. User-defined interfaces in PHP 5: Implementing (X)HTML Widgets
  2. Generic (X)HTML generation: defining the "HTMLRenderer" interface
  3. Working with interface implementers: defining (X)HTML widget classes
  4. Object-based (X)HTML rendering: more (X)HTML widgets to define
  5. Completing the list of (X)HTML widgets: defining classes for rendering headers and forms
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
December 26, 2005

print this article



Object-oriented page generation may be easily done through the implementation of simple classes that render a specific (X)HTML page element. Considering this concept, elements such as tables, divs, paragraphs or forms can be conceived as conceptual objects that expose a set of properties and methods, aimed primarily at creating conjunctly the structure of a web page.

For these objects to be functional and render the corresponding presentational structure within a web document, first I'll declare an interface called "HTMLRenderer", which exposes a single abstract method called "toHTML()". According to what you learned in the first article, this method will define the generic functionality of an object to present itself as capable of rendering (X)HTML markup.

Certainly this capacity is mandatory on (X)HTML widget objects, so each corresponding class will implement the "HTMLRenderer" interface by using the "toHTML()" method, tasked with rendering a specific web page element. However, the interface may be implemented by other objects of different family types that are capable of delivering HTML markup in some way.

So, considering the above concepts, I'll start by declaring the "HTMLRenderer" interface, which looks like this:

// interface HTMLRenderer
// defines generic method toHTML()
interface HTMLRenderer {
    public abstract function toHTML();

As you can see, the above listed interface presents only one abstract method, "toHTML()", that defines the generic functionality of an object, which will be explicitly declared on the classes implementing the interface. As one expects, a quick look at the abstract method suggests the functionality that must be explicitly defined within each class that uses the interface.

Having defined the simple structure of the interface, the next step is to write the (X)HTML widget classes that comprises the core logic for generating web pages, so turn your attention to the next few lines, where they will be defined.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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