Home arrow PHP arrow Page 5 - Object Interaction in PHP: Introduction to Composition

Working with the classes: a quick example - 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.

TABLE OF CONTENTS:
  1. Object Interaction in PHP: Introduction to Composition
  2. Composition: some theory ahead
  3. Composition in a practical sense: a basic example
  4. The “Table” class: a simple row-color alternator
  5. Working with the classes: a quick example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 29
July 11, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 3 - Follow our Sitemap

Dev Shed Tutorial Topics: