HomePHP Page 4 - Object Interaction in PHP: Introduction to Composition
The “Table” class: a simple row-color alternator - PHP
Composition is an important concept in PHP. It occurs when an object creates another object; that is, the first object completely possesses the second object. In this article (the first of two parts), Alejandro Gervasio explains composition, and shows some examples to illustrate his points.
Once we’ve deeply explored the definition for our “Page” class, it’s time to see how the “Table” class is defined, in order to complete the interaction process between the two classes. Thus, the “Table” class definition is listed below:
class Table { var $page; var $content; var $id; function Table(&$page,$content){ $this->page=&$page; $this->content=$content; $this->id='defaultID'; } function setId($id){ $this->id=$id; } function build($colorA,$colorB){ $this->page->html.='<table id="'.$this->id.'" width="100%">'; $i=0; foreach($this->content as $row){ $bgcolor=($i%2)?$colorA:$colorB; $this->page->html.='<tr bgcolor="'.$bgcolor.'"> <td>'.$row.'</td></tr>'; $i++; } $this->page->html.='</table>'; return $this->page->html; } }
As the above code shows, the unique function of this class is to build the main section of the page, generating a regular HTML table that alternates its row colors, to give a more polished presentation to the content. Data members are very descriptive and certainly don’t offer particular difficulties to understanding their meaning inside the context of the class, so let’s jump quickly into the explanation.
The first data member, that is $page, is the most relevant element since it represents a reference to an instance of the “Page” class. Remember that this object was already passed as an argument, at the time the “Table” object was created by the “Page” class. The two additional data members, $content and $id, refer the content to be included into the table and an ID table attribute, only for CSS styling purposes. Nothing unexpected, right?
Please notice how the constructor accepts the reference of the “Page” object and sets up the proper parameter initialization:
function Table(&$page,$content){ $this->page=&$page; $this->content=$content; $this->id='defaultID'; }
However, a question comes up here: why are we passing a reference of a “Page” object inside the class? The reason should become clear if we briefly look at the other core method that the class presents: the “build()” method. It takes two incoming parameters, $colorA and $colorB respectively, to generate the alternating table rows, but the process directly assigns its output to the HTML code of the “Page” class, as shown below:
function build($colorA,$colorB){ $this->page->html.='<table id="'.$this->id.'" width="100%">'; $i=0; foreach($this->content as $row){ $bgcolor=($i%2)?$colorA:$colorB; $this->page->html.='<tr bgcolor="'.$bgcolor.'"> <td>'.$row.'</td></tr>'; $i++; } $this->page->html.='</table>'; return $this->page->html; }
Can you see how this method modifies the $html property originally defined in the “Page” class? Just use the expression:
$this->page->html
and that’s it. Now, the final rendered table is appended as part of the overall page’s output, and then returned to the occurrence where the method was invoked. It’s really simple, don’t you think so?
Okay, now we have moved forward in order to gain a better understanding of composition in PHP. But there are still a couple of points left yet. Let’s see how we can implement these classes in a quick example, after listing the full source code for each class.