PHP
  Home arrow PHP arrow Object Interaction in PHP: Introduction to Composition, conclusion
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, conclusion
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 16
    2005-07-18


    Table of Contents:
  • Object Interaction in PHP: Introduction to Composition, conclusion
  • Composition in a practical sense: building a MySQL wrapping class
  • The other side of composition: the “Result” class
  • Assembling the whole picture: putting the classes to work

  • 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, conclusion
    ( Page 1 of 4 )

    In the first article of this series, the concept of composition in object-oriented programming was explained, and illustrated with an example that implemented composition using two simple classes. In this article, we will create a MySQL database wrapping class that uses the concept of composition.

    Introduction

    Welcome to the second part of the series “Object Interaction in PHP: Introduction to Composition.” Please don’t feel intimidated by that lengthy title. If you’ve been working with classes for a while, then you probably have a strong knowledge of what composition is about. However, for those developers just beginning to work with object-oriented programming in PHP, let’s explain briefly some definitions, in order to refresh some relevant concepts about the subject.

    Speaking in general terms, composition occurs when one object possesses completely another object. We might say that the first object is directly responsible for the creation of the second one. Definitely, this technique within the OOP paradigm, has proven to be highly successful on medium or large websites, where a object-oriented approach is often needed.

    In my previous article, I presented an illustrative example that implemented composition by using two simple classes, a “Page” class and a “Table" class, where this last one was directly created by the first one. The main purpose of the “Page” class was generating a regular HTML page, building the body section of the page using the functionality offered by the “Table” class, and rendering a row color-alternated table. Let’s take a quick look at these classes, so we can recapitulate what we’ve learned until now. The classes were defined like this:

    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;

                }

    }

    Next, the second class looked like this:

    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 you can appreciate from the above code, our “Page” class fully owns the second “Table” object, since it creates an instance of it within its “makeBody()” method, in order to build the HTML table. Going back to the concept of composition, we might say that “Table” composes the “Page” object. At this time, I think that the issue was correctly understood. One possible implementation for these classes would be the following:

    // 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();

    Simple and instructive, right? The above example creates our HTML page, including into its main section some content rendered in a table that displays rows using alternating colors. Fine, should we feel that composition has been completely mastered now? I don’t think so.

    We need to put this technique to work, building an application that is used very frequently. In order to see how composition can be implemented in the real world, we’ll create a MySQL database wrapping class that uses the concept previously explained. What do you think? Doe this sound interesting? Good, let’s put our abilities to the test and start writing the class.



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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