Object Interaction in PHP: Introduction to Composition - Working with the classes: a quick example (
Page 5 of 5 )
For those developers who want to play with the classes and get a more intimate knowledge of composition, here’s the full code for the classes reviewed here. First, our “Page” class:
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;
}
}
And, second, the “Table” class:
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;
}
}
Also, here’s a possible implementation for the classes, in order to generate a basic page where its main content section includes a table with alternating row colors:
// 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();
That’s all we need to make the classes work. As we usually do when working with classes, they’re included in the code. Then, we instantiate a “Page” object and build the page header, like this:
require_once 'pageclass.php';
require_once 'tableclass.php';
// instantiate a new Page object
$page=&new Page();
// make header
$page->makeHeader('<div>Header</div>');
Now, we simply pass to the “Table” object a range of numbers from 0 to 20, just to populate the main section of the page with some data, and assign an ID attribute to the table:
// set Table object parameters
$table=$page->makeBody(range(0,20));
$table->setId('maincontent');
Finally, we build the table, specifying the background colors that we want to be displayed alternately, generating the “footer” section, and displaying the page in the browser:
$table->build('#ffcc00','#eeeeee');
// make footer
$page->makeFooter('<div>Footer</div>');
// display page
echo $page->display();
Now, we can say that our job is finally finished. With the classes working seamlessly, it’s time to read the conclusions and look ahead to the next article in this tutorial about composition. Just follow me.
Wrapping up
In this first part of the series, we’ve tasted the power of composition in PHP, building two basic classes that demonstrate in a didactical way how one object composes another. This is useful for getting you started quickly working with other interesting aspects of object-oriented programming.
However, until now, we’ve worked merely with a couple of classes, which will hardly find their role in production environments. With this idea in mind, in the second part we’ll implement composition using a more realistic approach, since we’ll work with a fully functional MySQL abstraction class. Meanwhile, feel free to use the above developed code to introduce composition into your applications or develop your own classes. Until next time, stay tuned.