In the preceding section, you looked at inheritance—a way to build new classes upon existing ones, thus allowing you to reuse and extend what has already been defined and debugged.
This section looks at code reuse through two powerful methods commonly used in object-oriented programming, namely composition and aggregation.
Using the functionality of an object from within another object allows you to leverage existing functionality, rather than creating it from scratch. One way to accomplish such an interaction is through composition.
Note
Composition takes place when one object contains another object, meaning that the owning object is responsible for creating the contained object, and once the owner is destroyed, the contained object is destroyed as well.
The best way to understand how composition works in PHP is by example. Imagine that you want to replace the default login form used in the MyAuthOrcl
class discussed in the preceding section with...