PHP
  Home arrow PHP arrow Page 3 - 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? 
Google.com  
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 - Composition in a practical sense: a basic example
    ( Page 3 of 5 )

    As a introduction to composition in PHP, we’ll see a rather basic example that shows how one object composes another one. Since we’re speaking about object interaction, we need at least two objects to work with. So, to properly understand the basics of composition, let’s define two classes that comes handy to clarify any possible question.

    The first class that we’ll define for our example is simply a “Page” class, which shows several methods for building a simplistic Web page. Based on the classic page schema composed of  “Header,” “Body” and “Footer” sections, the class presents a specific method to create each one of them. Here’s how our “Pager” class looks:

    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;
        
    }
    }

    As you can see, the class is very simple and clear to understand. As mentioned before, it presents four methods concerned with building a web page, defined as follows: “makeHeader()”, makeBody()”, “makeFooter()” and finally “display()”, which simply returns the complete HTML markup to be dumped directly to the browser.

    The “Page” class contains only two data members, necessary to generate the entire page: $title and $html. As you might guess, the first property will be used to display the page’s title, while the second will store the progressive HTML output, to be finally echoed to the browser. In fact, there is not much to be said about the basic “makeHeader()” and “makeFooter()” methods, since they speak for themselves.

    Because of their simplicity, we’ll just say that they take care of creating the header and footer page sections, and add a few regular tags to wrap up the content of each section, which is passed as an argument to every specific method. However, not everything is as boring as seems. If we take a closer look at the “makeBody()” method, we quickly realize that it’s quite special. Why? Well, the method accepts a single parameter, $content that is defined as an array structure, in order to generate the main section of the page

    But, it’s doing something else. Notice that it’s returning a new object called “Table”, as we can appreciate in the following line:

    return new Table($this,$content);

    This looks very interesting. Has it caught your attention? Fine. Here we have a “Page” class which is directly creating an instance of another “Table” object. Given this condition, we can say that the “Table” object composes the “Page” object. Pretty good, right? Just ask yourself the question previously mentioned: what happens to the “Table” object if the “Page” object ever dies? Certainly, it will die too, so it’s not capable of surviving on its own as an independent object. Thus, having answered that question, we have one object that composes another one.

    Although the method is very simple, it shows clearly how we’ve defined the interaction process between the two objects. Notice that the newly created “Table” object accepts two parameters for its constructor: the first is the “Page” object itself, referenced as $this, while the second is the $content array used to include some content in the main section of the page.

    The reason for creating a new “Table” object answers mostly to the example’s purposes. We want each “Page” object to create a “Table” object, which will render a color-alternated HTML table and display the contents in this container. Definitely, this page generator class isn’t suitable for application in real conditions, but it does its job by showing a clear situation where object composition takes place.

    At this point, you’re probably wondering, what does the “Table” class look like? Okay, I thought you’d never ask. So, let’s have a look at its structure. Just click to the next page.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek