PHP
  Home arrow PHP arrow 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 / 28
    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
    ( Page 1 of 5 )

    Composition is an important concept in PHP. It occurs when an object creates another object; that is, the first object completely possesses the second object. In this article (the first of two parts), Alejandro Gervasio explains composition, and shows some examples to illustrate his points.

    Introduction

    From a PHP programmer’s point of view, there are many interesting ways that objects can interact with each other within an application. Used extensively across well-structured projects, inheritance is one of the major foundations of the object-oriented paradigm. It allows a  basic and generic functionality to be implemented on a base class, and provide generic methods and properties that are inherited by any derived subclass, in order to “fine-tune” the child classes to suit more specific purposes. If you’ve been working for a while with database abstraction classes, to cite an illustrative example, probably you know what I’m talking about here.

    Aside from inheritance, and its partner, polymorphism, we can find other situations where objects are involved in some kind of interaction. Definitely, one concept that rapidly comes to my mind is that of aggregation. If you’re unaware of its existence, you’re missing something really relevant, trust me.

    Generally speaking, aggregation occurs when one object uses the functionality of another object to satisfy its own purposes. Although this sounds a bit egotistical, the truth is that aggregation is an extremely powerful concept, used very frequently in object-oriented environments. Commonly, there are situations where one object, such as a database result set processor, needs to have database connectivity within its scope. This is a common case where this last object performs a query against a database, using the connectivity given by the first object, which is usually passed to the second as a constructor parameter.

    Definitely, this might sound familiar to you, if you work with objects in PHP on a daily basis. However, the subject is really very far from being fully covered. I hope you're wondering why. What about having an object that creates another object, that is, the first object completely possesses the second? Yes! Now we are talking about a concept often known as composition.

    So, do we have yet another big player in the OOP arena? You bet. What’s more, composition plays a very important role in many situations, where the interaction between objects is carried out in a carefully planned way. Thus, the subject deserves an indepth examination. That’s what I’ll do in the course of this article. I’ll offer a short introduction to the theory of composition, as well as its possible implementation in different practical situations. I guess that you’re ready to learn more about it, right? Okay then, let’s not waste any more time in preliminaries.



     
     
    >>> 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 6 hosted by Hostway
    Stay green...Green IT