HomePHP User-defined interfaces in PHP 5: Implementing (X)HTML Widgets
User-defined interfaces in PHP 5: Implementing (X)HTML Widgets
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.
Welcome to the second part of the series "User-defined interfaces in PHP5." By way of returning to the first article, I explained the basics for using interfaces within a PHP5 application. I also provided you with a pair of sample classes that implemented the same "DeSerializer" interface. As you surely remember, the interface's abstract methods were explicitly defined to work with byte-stream representations of data.
In addition to implementing a functional example for utilizing the sample classes, some key concepts related to interfaces were introduced as part of the tutorial. By combining the underlying theory together with the practical usage, hopefully you now understand the way in which interfaces are implemented in PHP5.
Over this second part of the series, I'll go further into the implementation of interfaces, by introducing several classes aimed specifically at generating (X)HTML "widgets" as a method of generating web pages from an object-oriented point of view. The use of independent programming structures for creating (X)HTML page objects is yet another method for building web pages focused primarily on the handled data rather than on the overall page layout used in template-based systems.
Generally, presentational widgets -- including (X)HTML widgets -- offer a greater level of flexibility for larger web programs because they allow you to define application layers in a very clear way. On top of the application, the data layer is defined (comprised of backend databases), then probably a data access layer is added (usually implemented through a persistent layer) between the databases and the core logic layer. Finally the presentational layer is set up, which receives the data and uses the widgets to generate the user interface.
The drawback to the "widget" approach resides mainly in the performance cost of creating many objects for rendering a web document, even if they only exist until the page has finished displaying.
However, and leaving out the pros and cons of presentational widgets, I'll explain a more complex usage of interfaces by associating several (X)HTML widget classes to a couple of the MySQL processing classes, in this way demonstrating how objects of different types can be "hooked up" to each other through a common shared interface, in a real application.
Having defined the goals of this second tutorial, let's get started.