Introduction Welcome to the second part of the series “Object Interaction in PHP: Introduction to Composition.” Please don’t feel intimidated by that lengthy title. If you’ve been working with classes for a while, then you probably have a strong knowledge of what composition is about. However, for those developers just beginning to work with object-oriented programming in PHP, let’s explain briefly some definitions, in order to refresh some relevant concepts about the subject. Speaking in general terms, composition occurs when one object possesses completely another object. We might say that the first object is directly responsible for the creation of the second one. Definitely, this technique within the OOP paradigm, has proven to be highly successful on medium or large websites, where a object-oriented approach is often needed. In my previous article, I presented an illustrative example that implemented composition by using two simple classes, a “Page” class and a “Table" class, where this last one was directly created by the first one. The main purpose of the “Page” class was generating a regular HTML page, building the body section of the page using the functionality offered by the “Table” class, and rendering a row color-alternated table. Let’s take a quick look at these classes, so we can recapitulate what we’ve learned until now. The classes were defined like this: class Page { var $title; var $html; function Page($title='Default Page'){ $this->title=$title; $this->html=''; } function makeHeader($header){ $this->html.='<html><head><title>'.$this->title.'</title></head><body>'.$header; } function makeBody($content=array()){ return new Table($this,$content); } function makeFooter($footer){ $this->html.=$footer.'</body></html>'; } function display(){ return $this->html; } } Next, the second class looked like this: 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 you can appreciate from the above code, our “Page” class fully owns the second “Table” object, since it creates an instance of it within its “makeBody()” method, in order to build the HTML table. Going back to the concept of composition, we might say that “Table” composes the “Page” object. At this time, I think that the issue was correctly understood. One possible implementation for these classes would be the following: // include the classes require_once 'pageclass.php'; require_once 'tableclass.php'; // instantiate a new Page object $page=&new Page(); // make header $page->makeHeader('<div>Header</div>'); // set Table object parameters $table=$page->makeBody(range(0,20)); $table->setId('maincontent'); // build body table $table->build('#ffcc00','#eeeeee'); // make footer $page->makeFooter('<div>Footer</div>'); // display page echo $page->display(); Simple and instructive, right? The above example creates our HTML page, including into its main section some content rendered in a table that displays rows using alternating colors. Fine, should we feel that composition has been completely mastered now? I don’t think so. We need to put this technique to work, building an application that is used very frequently. In order to see how composition can be implemented in the real world, we’ll create a MySQL database wrapping class that uses the concept previously explained. What do you think? Doe this sound interesting? Good, let’s put our abilities to the test and start writing the class.
blog comments powered by Disqus |
|
|
|
|
|
|
|