PHP
  Home arrow PHP arrow Page 4 - Object Interaction in PHP: Introduction to Composition
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Object Interaction in PHP: Introduction to Composition
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 29
    2005-07-11


    Table of Contents:
  • Object Interaction in PHP: Introduction to Composition
  • Composition: some theory ahead
  • Composition in a practical sense: a basic example
  • The “Table” class: a simple row-color alternator
  • Working with the classes: a quick example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Object Interaction in PHP: Introduction to Composition - The “Table” class: a simple row-color alternator
    ( Page 4 of 5 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT