PHP
  Home arrow PHP arrow Page 3 - Object Interaction in PHP: Introduction to Aggregation, part 1
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 Aggregation, part 1
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 23
    2005-05-25


    Table of Contents:
  • Object Interaction in PHP: Introduction to Aggregation, part 1
  • What is aggregation?
  • Applying aggregation in a practical manner: defining sample classes
  • A closer look at aggregation: the boosted "dataMailer" class
  • Aggregation in action: a concrete 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 Aggregation, part 1 - Applying aggregation in a practical manner: defining sample classes
    ( Page 3 of 5 )

    Let's define two basic classes and see how they can interact, in order to understand how aggregation works. First, we create a basic "arrayProcessor" class, which provides a common interface to perform regular array operations, instead of using the native PHP functions. Here is its definition:

    class arrayProcessor{

    var $data;

    function arrayProcessor($data=array()){

    (is_array($data))?$this->data=$data:die('Invalid parameter '.$data);

    }

    function getFirstElement(){

    return reset($this->data);

    }

    function getCurrentElement(){

    return current($this->data);

    }

    function getLastElement(){

    return end($this->data);

    }

    function getPreviousElement(){

    return prev($this->data);

    }

    function getNextElement(){

    return next($this->data);

    }

    function reverseElements($prekeys=true){

    return array_reverse($this->data,$prekeys);

    }

    function sortElements(){

    sort($this->data);

    return $this->data;

    }

    function getRange($offset,$length){

    return array_slice($this->data,$offset,$length);

    }

    function getRandomElement(){

    shuffle($this->data);

    return $this->data[0];

    }

    function searchElement($keyword){

    return array_search($keyword,$this->data);

    }

    function countElements(){

    return count($this->data);

    }

    }

    As you can see, our "arrayProcessor" class presents several clever methods for manipulating arrays for different purposes, and accepts only one parameter, $data, which is assigned a default array value. Also, I've defined the most common array operations (of course you can add your own), so each method is designed specifically to handle array elements in some manner. By simply instantiating an "arrayProcessor" object and utilizing its methods, we're able to move back and forward within the structure, reverse, sort and extract elements, and so forth.

    If we take a moment and analyze the above defined class, it seems like we're implementing an already existing PHP functionality. Why go to the trouble of designing such a class, when we can directly use the native array functions? The truth is that we're providing a single interface to access array elements, focusing mainly on the handled data and hiding any internal processing from outside. That's what makes objects behave as "black boxes," and naturally "pluggable" block elements.

    Okay, we were talking about object aggregation, right? So, we need to define the other sample class, to see in detail how both classes can work together. Therefore, the next step consists of defining the second class, which simply takes care of sending by email a hypothetical newsletter to several possible recipients. This newly created class, named "dataMailer" is defined as follows:

    class dataMailer {

    var $arrayProc;

    var $subject;

    var $message;

    var $headers;

    function dataMailer(&$arrayProc){

    $this->arrayProc=&$arrayProc;

    $this->subject='Object-Oriented PHP NewsLetter';

    $this->message='Hi, this is the ultimate Object-Oriented PHP newsletter!';

    $this->headers='MIME-Version: 1.0'."\r\n".'Content-type: text/html; charset=iso-8859-1'."\r\n";

    }

    function setSubject($subject){

    $this->subject=$subject;

    }

    function setMessage($message){

    $this->message=$message;

    }

    function setHeaders($headers){

    $this->headers=$headers;

    }

    function sendData($firstRecip,$numberRecip){

    $recipients=&$this->arrayProc->getRange($firstRecip,$numberRecip);

    foreach($recipients as $recipient){

    if(!mail($recipient,$this->subject,$this->message,$this->headers)){

    die('Failed to send mail to recipient :'.$recipient);

    }

    }

    }

    }

    Because of its simplicity, the class is really easy to understand. But, let's take a detailed look at what it does, so that implementing its usage will be even clearer.

    The class displays several data members that will be explained in turn. However, let's stop for a while at the first member, $arrayProc, because it's the key element for making this second class work. In this case, $arrayProc composes an instance of the "arrayProcessor" class, which is passed as the unique parameter to the constructor and assigned as a class property. Doing so, the "dataMailer" class is able to use all of the functional methods present in the first class. 

    This condition is better illustrated below, listing the class constructor:

    function dataMailer(&$arrayProc){

    $this->arrayProc=&$arrayProc;

    $this->subject='Object-Oriented PHP NewsLetter';

    $this->message='Hi, this is the ultimate Object-Oriented PHP newsletter!';

    $this->headers='MIME-Version: 1.0'."\r\n".'Content-type: text/html; charset=iso-8859-1'."\r\n";

    }

    In this situation, "dataMailer" aggregates the "arrayProcessor" object, clearly showing aggregation's real power, since within the second class, we're accessing the methods provided by "arrayProcessor."

    One point worth considering is that we're passing a reference of the instance of the first class (notice the usage of the & ampersand operator, preceding the object), something commonly used in this type of interaction.

    Now, can you see the implicit possibilities that this object aggregation process offers to us? With such a powerful interaction, certainly we're able to implement a few classes, make them interact in this way, and build up well-defined structures for Web applications.

    Once we've explained how an object aggregates another object, let's go back to the "dataMailer" class, to define the rest of the class data members. First we define the $subject property, that means the subject of the email message to be sent. Next, we have $message, which defines the body of the message, and finally $headers that, as the name clearly suggests, composes the MIME headers to be send along with the message. Simple and straightforward, yes?

    However, we're just beginning to see the power of aggregation. Let's go deeper in the structure of our "dataMailer" class to see how it uses the functionality of "arrayProcessor." Keep reading.



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