Home arrow PHP arrow 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.

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

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.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: