Working with interface implementers: defining (X)HTML widget classes - 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.
As I explained previously, (X)HTML widget classes present a very simple (but powerful) structure. Since they implement the "HTMLRenderer" interface, all of them must provide a specific definition for the "toHTML()" method, in order to fit the requirements of interfaces, as I explained in the first article.
Given that, I'll define the first widget class, called "Table", which as the name implies, generates a regular HTML table:
// class Table class Table implements HTMLRenderer{ private $output='<table '; private $data=array(); private $attributes=array(); public function __construct($attributes=array()){ if(count($attributes)<1){ throw new Exception('Invalid number of attributes'); } $this->attributes=$attributes; } public function setData($data=array()){ if(count($data)<1){ throw new Exception('Empty data set'); } $this->data=$data; } public function toHTML(){ foreach($this->attributes as $attribute=>$value){ $this->output.=$attribute.'="'.$value.'" '; } $this->output=substr_replace($this->output,'>',-1); foreach($this->data as $data){ $data=($data instanceof HTMLRenderer)?$data->toHTML():$data; $this->output.='<tr><td>'.$data.'</td></tr>'; } $this->output.='</table>'; return $this->output; } }
With the first widget class defined, let's dissect its code to understand each pertinent method.
As you can see through its definition, the "Table" class implements the "HTMLRenderer" interface. This is done by simply specifying the "interface" keyword after the class name, thus it's not a difficult thing. Then, the constructor accepts an "attributes" array parameter, which contains the proper attributes associated with a regular HTML table, for assignment as a class property.
The next method to be analyzed is "setData()", which is the unique modifier exposed by the class that provides a single access point for populating the table with either static or dynamic data. Notice that it accepts an array as incoming data and sets it up as another class property. Because of its simplicity, I'll deliberately avoid a more detailed explanation.
Now, turn your attention to the "toHTML()" method, since it implements an effective logic and introduces the use of the "instance of" operator. As you can see, basically the method performs two loops. The first one iterates over the table's attributes and builds the opening <table> tag together with its attributes.
The second loop is the most interesting though. Notice the checking process performed on the "data" array through the "instance of" operator. What this operator does is check whether the current array element is an implementer of the "HTMLRenderer" interface. If this is true, then the element is an object that also presents the "toHTML()" method, so it's called accordingly. The lines below illustrate this concept:
foreach($this->data as $data){ $data=($data instanceof HTMLRenderer)?$data->toHTML():$data; $this->output.='<tr><td>'.$data.'</td></tr>'; }
If you study the expression above, it should be clear that the recursive implementation of objects using the "HTMLRenderer" interface allows you to easily generate nested HTML elements. To clarify things, say that an array of paragraph objects is passed to the class. Since they also have a "toHTML()"method, as a result they will be rendered inside of each table row. Isn't that simple and powerful?
Now that you've hopefully understood the advantages of applying recursive interface implementers, I'll move forward to review the rest of (X)HTML widget classes.