HomePHP Page 2 - Object Interaction in PHP: Introduction to Aggregation, part 1
What is aggregation? - 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.
In the context of object interaction, aggregation simply refers to a situation where one object is passed to another object to perform some operations or methods within the object that called it originally. Usually, the second object is passed to the first one as a parameter using one of its member methods.
The great benefit of this is that, with the second object living happily in the first one, it's possible to directly call its methods, allowing us to take advantage of its existing functionality, in order to fit the purposes of the first object.
In terms of practical application, aggregation is extremely important and powerful. After all, if I consider myself as an object A, which has a rather limited budget, but fortunately has a rich friend, that is, an object B, who is generously offering to me (at least temporarily) his credit cards, that is $B->offerCreditCards(), to buy that NAD state-of-the-art preamplifier, I'd feel very powerful!
If we talk about performance issues, the advantages of aggregation mainly come from its lower overload, since most of the time only one object is shared by other objects. However, this advantage might be discarded in the case of having a class for database connection shared by other multiple classes. You may run into difficulties if multiple database connections are established to the same server, causing a noticeable detriment to the system, particularly if your site is attracting many visitors.
Are you feeling as if it's a bit confusing to handle objects here and there? It's best to understand this by example. In the next section, we'll define two simple classes to illustrate the concept of aggregation in a practical sense.