PHP
  Home arrow PHP arrow Object Interaction in PHP: Introductio...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · This second part of the series makes room to implement Composition by writing...
       · Again, great article. Really learn a lot as I'm working through all your articles on...
       · Hello again Matthijs,Very grateful for the compliments. Also, I'm pleased to...
     

       

    PHP ARTICLES

    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - Viewing and Editing Tasks for a Project Mana...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway