Fork me on GitHub

article

More gettin’ my OOP on: Aggregation

June 21, 2005 | PHP Scripting

I promised myself that I’d write about something else but this has me so stoked that I just can’t keep it in.

One of my biggest problems with getting into large PHP application programming with OOP was the sharing of functions. Inheritance just wasn’t doing it the way I wanted it to. I knew it was possible but hadn’t gotten the hang of it until I found Aggregation. Sweet, sweet aggregation…

Serious $geek++; coming up

So you’re probably wondering what the hell I’m talking about. Unless you’re seriously into php you’ve probably already glassed over so why not head over to Chipotle.com and witness a very neat flash website. For the rest of you that have any kind of remote interest in PHP and OOP, hopefully you come back and read the rest of this.

What aggregation is not: anything related to agriculture.

What it is: a way to share object functions across objects. You can safely create a library of general functions and share their functionality within other objects wherever needed.

This is the code reuse that I’ve been looking for – I finally learned it.

Class Association

There are a couple different ways to aggregate objects. The first is by instatiating a class inside another class. For example:

class myDate
  {
    function setDate()
      {
        return date("Y-m-d");
      }
  }

class Writer
  {
    var $dt;

    function writeDate()
      {
        $this->dt = &new myDate();
    echo $this->dt->setDate();
      }
  }

// using the classes

$wr = new Writer();
$wr->writeDate();

// echoes the date to the browser

This example creates an instance of the setDate class inside the Writer class. This means that any variable manipulations made with this instance of myDate will stay within this instance of Writer. The variables will not be shared outside of the Writer class instance. Any other function that wants to use the myDate object will have to create its own instance. This method is good if all you need to do is share routines and not any variables that they create.

Object Association

The next example will share the function by passing it along as a reference. I’ll use the same example and modify it a bit:

class myDate
  {
    function setDate()
      {
        return date("Y-m-d");
      }
  }

class Writer
  {
    var $dt;

    function writeDate(&dt)
      {
        $this->dt = &dt;
    echo $this->dt->setDate();
      }
  }

// using the classes

$wr = new Writer();
$dt = new myDate();

$wr->writeDate(&dt);

// echoes the date to the browser

This example is very slightly different than the first example in that it shares the myDate object by passing the object pointer in by reference – this way there is only one instance of the myDate class running and all variable manipulations will be available across any other object that aggregates it in.

This is handy if you’re dealing with globally used vars in a larger application – variables created and manipulated by the aggregated object can be passed around to whatever other class pulls it in.

Conclusion

With these two methods of aggregation we have ways of manipulating shared and private variables, depending upon how aggregate the classes and have a nice way of pulling in classes that are simply data processors and who just have supporting functions in them.

I can see a whole new world opening up to me now. And since I’ve gotten a handle on OOP I haven’t looked back – I can’t stand the idea of procedural code unless its just a snippet in a page – otherwise it should be a class or a function.

So hopefully I haven’t bored you to tears, and maybe I’ve shown you something new. Either way, thanks for tuning in. Though I can see it now – there’s gonna be more comments on the Chipotle site than there were about this article. Did you see the dogs running circles around the office in that site? No? Go look again…

3 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  • You may be correct. The Chipotle site was kinda neat. As for what you tried to explain to us, well……….ZZzzzzzz…… 😛

    CrabbyCakes, June 22, 2005 12:49 am | permalink

  • man, i hope i never have the displeasure to have to be able to understand what the hell you are talking about :p

    tobo, June 22, 2005 8:57 am | permalink

  • I’m going to have to post an update to this now… passing objects by reference is now depreciated.

    Shawn Parker, July 9, 2005 1:37 pm | permalink

Comments are closed