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