PHP
  Home arrow PHP arrow Page 4 - 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 - A closer look at aggregation: the boosted "dataMailer" class
    ( Page 4 of 5 )

    As we've seen previously, our "dataMailer" class establishes a well-defined object interaction by accepting one instance of "arrayProcesssor." Besides displaying the corresponding modifiers for $subject, $message and $headers, the class presents another method: "sendData()," which is the real workhorse of the class. This method simply obtains the email addresses of each recipient from an array structure (more about this in a moment), builds up the structure of the message, adds the proper subject and the message headers, and finally sends the newsletter to the given email addresses.

    Here's where things get really exciting. Notice that this method accepts two parameters: $firstRecip and $numberRecips, respectively. Since we're obtaining email addresses from an array, the first argument represents the initial offset within the array, and the second means the numbers of email addresses extracted from it. But, how do we obtain this data? Well, if we have a look at the class code, we can see the following line:

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

    As you can see, here we're using the "getRange()" method of the "arrayProcessor" class to retrieve a range of email addresses for sending the newsletter. In other words, we're taking advantage of the functionality that is generously offered by the "arrayProcessor" class, utilizing one of its methods for the specific purposes of the "dataMailer" class. Now, are you starting to see the great benefits of aggregation? I hope so!

    The rest of the method is easy to follow. It simply iterates over the email addresses array and send the messages to the given recipients, like this:

    foreach($recipients as $recipient){

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

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

    }

    }

    Definitely, through the usage of the first object within the environment of the second, we've boosted its functionality. Since the structure of the "dataMailer" class is easily expandable, we might want to change the "sendData()" method to send a message to a randomly chosen recipient. The newly rewritten method would look something like this:

    function sendData(){

    $recipient=&$this->arrayProc->getRandomElement();

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

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

    }

    }

    Wasn't that easy? This time we're using the "getRandomElement()" provided by our "arrayProcessor" object to randomly select a lucky recipient for newsletter. As you can see, there are many possible ways to exploit the awesome capabilities of aggregation in PHP. It's just a matter of putting a couple of classes to work and making them interact. It's really fun.

    However, so far we've specifically focused our attention on the source code for our two brand new classes. It's time to show a concrete example to demonstrate the benefits of aggregation. So, let's jump right into the sample code.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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