HomePHP Page 5 - Object Interaction in PHP: Introduction to Aggregation, part 1
Aggregation in action: a concrete example - 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.
Armed with the above defined classes, we'll demonstrate with a basic yet illustrative example how to implement aggregation. First, let's define a simple text file that stores the email addresses corresponding to our newsletter's subscribers. The "subscribers.dat" file would look as simple as follows:
john@crazyprotocols.com
hellen@crazyprotocols.com
nancy@randomports.com
daniel@randomports.com
susan@bynarygifts.com
rachel@bynarygifts.com
It looks like we're not attracting many subscribers, but for the purposes of our example, this works. Okay, having defined our data file, let's email our visitors the newsletter:
// include the classes
require_once 'arrayprocessorclass.php';
require_once 'datamailerclass.php';
// read subscriber email addresses
$data=file('subscribers.dat');
// instantiate a new arrayProcessor object
$ap=&new arrayProcessor($data);
// instantiate a new dataMailer object and aggregate arrayProcessor object to dataMailer object
$dm=&new dataMailer($ap);
// send data to subscribers
$dm->sendData(0,6);
With these few lines of code, we're sending our newsletter. Although the code is clear enough, let's explain its logic. As usual, we include the proper class files for object instantiation. Then, we read the email addresses from the text file previously created, storing them in the $data array:
$data=file('subscribers.dat');
The next task consists of instantiating the objects and perform the aggregation process, passing the "arrayProccessor" object to the constructor of "dataMailer," as indicated below:
// instantiate a new arrayProcessor object
$ap=&new arrayProcessor($data);
// instantiate a new dataMailer object and aggregate arrayProcessor object to dataMailer object
$dm=&new dataMailer($ap);
Lastly, we email the newsletter by using the "sendData()" method, specifying the starting point within the $data array, and the number of elements to be processed. In this case, we've decided to process all of the email addresses, passing the values 0 and 6 to the method, as follows:
$dm->sendData(0,6);
If we ever wanted to send data to only the first three subscribers, the following expression should be used:
$dm->sendData(0,3);
Despite the example's simplicity, it shows how easily object aggregation can be implemented. However, these are a just a few ideas to get you started. I'm pretty sure you can think up some more interesting examples to "aggregate" this functionality in your next project.
Conclusion
At this point, we have been exploring the arena of object interaction, digging into the concept of aggregation, as well as its possible uses in different scenarios. While the examples shown present a clear, easy-to-follow logic, they are fairly good to get you started on the subject with only minor hassles.
But, don't feel as if this is the end of the story. There are still more items to be reviewed. In the next part of this series, we'll implement aggregation in a rather more complex condition, to solve some frequent problems that most developers face on a regular basis, such as building up a paging class that aggregates a MySQL abstraction class. In the meantime, feel free to use the given classes and make them interact!