Home arrow PHP arrow 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.

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

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:

class arrayProcessor{

var $data;

function arrayProcessor($data=array()){

(is_array($data))?$this->data=$data:die('Invalid parameter '.$data);

}

function getFirstElement(){

return reset($this->data);

}

function getCurrentElement(){

return current($this->data);

}

function getLastElement(){

return end($this->data);

}

function getPreviousElement(){

return prev($this->data);

}

function getNextElement(){

return next($this->data);

}

function reverseElements($prekeys=true){

return array_reverse($this->data,$prekeys);

}

function sortElements(){

sort($this->data);

return $this->data;

}

function getRange($offset,$length){

return array_slice($this->data,$offset,$length);

}

function getRandomElement(){

shuffle($this->data);

return $this->data[0];

}

function searchElement($keyword){

return array_search($keyword,$this->data);

}

function countElements(){

return count($this->data);

}

}

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!';

$this->headers='MIME-Version: 1.0'."\r\n".'Content-type: text/html; charset=iso-8859-1'."\r\n";

}

function setSubject($subject){

$this->subject=$subject;

}

function setMessage($message){

$this->message=$message;

}

function setHeaders($headers){

$this->headers=$headers;

}

function sendData($firstRecip,$numberRecip){

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

foreach($recipients as $recipient){

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

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!';

$this->headers='MIME-Version: 1.0'."\r\n".'Content-type: text/html; charset=iso-8859-1'."\r\n";

}

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.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

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

 


Dev Shed Tutorial Topics: