Home arrow PHP arrow Page 4 - Object Interaction in PHP: Introduction to Aggregation, part 1

A closer look at aggregation: the boosted "dataMailer" class - PHP

Aggregation in PHP allows one object to use another object. It's a very powerful concept. This article, the first in the series, serves as an introduction to some of the things you can do with aggregation.

TABLE OF CONTENTS:
  1. Object Interaction in PHP: Introduction to Aggregation, part 1
  2. What is aggregation?
  3. Applying aggregation in a practical manner: defining sample classes
  4. A closer look at aggregation: the boosted "dataMailer" class
  5. Aggregation in action: a concrete example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 23
May 25, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: