HomePHP 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.
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:
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:
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:
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.