PHP
  Home arrow PHP arrow Page 5 - Object Interaction in PHP: Introduction to Aggregation, part 1
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Object Interaction in PHP: Introduction to Aggregation, part 1
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 23
    2005-05-25


    Table of Contents:
  • Object Interaction in PHP: Introduction to Aggregation, part 1
  • What is aggregation?
  • Applying aggregation in a practical manner: defining sample classes
  • A closer look at aggregation: the boosted "dataMailer" class
  • Aggregation in action: a concrete example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Object Interaction in PHP: Introduction to Aggregation, part 1 - Aggregation in action: a concrete example
    ( Page 5 of 5 )

    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!



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT