HomePHP Page 3 - Object Interaction in PHP: Introduction to Aggregation, part 1
Applying aggregation in a practical manner: defining sample classes - 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.
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:
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!';
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!';
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.