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



     
     
    >>> 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 4 Hosted by Hostway
    Stay green...Green IT