HomePHP Object Interaction in PHP: Introduction to Aggregation, part 1
Object Interaction in PHP: Introduction to Aggregation, part 1
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.
In the growing world of Object Oriented Programming, the ways that objects interact with each other involve directly working with several concepts that deserve an in-depth look, in order to take advantage of their power in real-world applications. Particularly when we're dealing with PHP, which is definitely an easy-to-grasp language compared to the harder learning curves of Java or C++ (among others), things are a lot more accessible, even if we go beyond the limits of working with procedural solutions.
At this point, most PHP users have already experienced the nice capabilities of PHP classes, by taking either the do-it-yourself road or reusing existing, well-trusted classes for managing larger projects. However, there are times where we need to work with several classes that require a strong interaction to efficiently accomplish a given set of tasks. So, this relationship immediately brings up a question: how do objects interact? Fortunately, the answer is quite straightforward, since this interaction can be defined within well-known OOP pillars.
The first trusted friend is inheritance, which offers a powerful mechanism for building class hierarchies, and allows us to address specific issues in a number of subclasses without the need to recreate the logic or functionality that already exists in the super class or parent class. That sounds very familiar, doesn't it?
The next big ally of inheritance is polymorphism, which is defined as the ability of different classes to behave differently for the same operation. In programming lingo, we can say that two or more classes are sharing the same interface. Probably its most common application is found within database abstraction classes, where we're using the same method (i.e. connect) for connecting to different databases systems.
So, that's all there is to know about object interaction? Well, not yet. Aside from inheritance and polymorphism, there are a couple of interesting ways to make objects interact. Maybe you've been working with an object that uses another object, right? Or perhaps worked with an object that creates another one? Sure! We're referring to the concepts of aggregation and composition. These two additional forms of object interaction are extremely useful, giving us undoubtedly strong foundations for deeply exploiting the power of object-oriented programming.
However, all of these things sound like a bunch of buzzwords, with no immediate use in Web applications, right? Well, not so fast. We have the power and need to use it. In this article, we'll introduce ourselves to the process of implementing aggregation in PHP, to help us learn more about the possible ways to make object interaction work for us. Let's get started.