In this chapter
We'll learn the four ways objects can be connected — association, aggregation, composition and dependency — what each means for who creates and who owns an object, why a booking should point to its customer instead of copying her phone number, and how the relationships are drawn in UML class diagrams.
The Problem in Real Life
Clare Lefèvre changes her phone number on Monday. Her reminders for Wednesday and Friday still go to the old one.
Mia finds out why: Booking's constructor takes the customer's name and phone as strings and keeps its own copies. The Customer object has the new number; the two bookings still have the old one. Nothing connects them.
Clare is one person. Why does every booking have its own copy of her?
Mia
Copying Data vs. Pointing to the Object
Copies go stale
A booking that copies the customer's phone keeps the old number forever. One customer, many out-of-date copies.
Connected objects
A booking can hold the Customer object itself. When the customer changes, every booking sees it.
Who owns whom?
A customer exists without bookings; a booking's lines don't exist without the booking. That difference has a name.
Composition, Aggregation, Association and Dependency
How objects are connected: real programs are many objects working together. The connections come in four strengths, from loosest to tightest:
- Dependency: an object uses another for a moment.
- Association: an object knows another and keeps a reference to it.
- Aggregation: an object has others that also live on their own.
- Composition: an object owns parts that live and die with it.
Objects hold references, not copies: a property can hold an object: private Customer $customer. What it stores is a reference to the customer object — a handle that points at it (chapter 13 explains handles exactly). Two bookings built with the same Customer object point at the same customer. That's Mia's fix: Booking takes a Customer, not a name and a phone string, and asks it for the phone when the reminder is sent: $this->customer->phone(). Change the customer once, and every booking sees the new number. Copy values only when you want a snapshot that must not change — like the price that was charged (chapter 6).
<?phpdeclare(strict_types=1);class Customer{public function __construct(public string $name, public string $phone) {}}class Booking{public function __construct(private string $customerName, private string $phone) {}public function reminderTo(): string{return $this->phone; // a copy taken when the booking was made}}$clare = new Customer("Clare Lefèvre", "+447700900456");$wednesday = new Booking($clare->name, $clare->phone);$clare->phone = "+447700900999"; // Clare changes her numberecho $wednesday->reminderTo(), "\n"; // +447700900456 — the old one
<?phpdeclare(strict_types=1);class Customer{public function __construct(public string $name, public string $phone) {}}class Booking{public function __construct(private Customer $customer) {}public function reminderTo(): string{return $this->customer->phone; // asks the customer at send time}}$clare = new Customer("Clare Lefèvre", "+447700900456");$wednesday = new Booking($clare);$friday = new Booking($clare); // the same customer object$clare->phone = "+447700900999";echo $wednesday->reminderTo(), " ", $friday->reminderTo(), "\n";// +447700900999 +447700900999
Dependency: the loosest connection: an object uses another one briefly, usually because it's passed in as a method parameter, and doesn't keep it. A ReminderSender that receives a Notifier in its send() method depends on Notifier for that one call. If Notifier's method changes, ReminderSender may have to change too — that's what "depends on" means.
<?phpdeclare(strict_types=1);interface Notifier{public function send(string $to, string $text): string;}class SmsNotifier implements Notifier{public function send(string $to, string $text): string{return "SMS to $to: $text";}}class ReminderSender{public function remind(Notifier $notifier, string $to): string // used, not kept{return $notifier->send($to, "See you tomorrow.");}}echo (new ReminderSender())->remind(new SmsNotifier(), "+447700900456"), "\n";// SMS to +447700900456: See you tomorrow.
Association: an object knows another and keeps a reference in a property: a Booking knows its Barber. Both exist independently, and neither creates the other. Association can go one way (the booking knows the barber, the barber doesn't list bookings) or both ways; one way is simpler, because there's only one link to keep correct.
Aggregation and composition: both are "has-a" relationships, and the difference is ownership. In aggregation, the parts exist on their own and can be shared: a Barber's Shop has barbers, but a barber exists before the shop hires him and after he leaves; the customer of a booking exists without the booking. In composition, the whole owns its parts, which make no sense alone and live and die with it: a booking's lines ("Haircut £25.00", "Hot towel £2.50") are created by the booking and belong to no one else. A practical sign: in composition, the whole creates its parts itself (new BookingLine(...) inside Booking); in aggregation and association, the other objects are passed in from outside.
<?phpdeclare(strict_types=1);final readonly class BookingLine{public function __construct(public string $service, public int $pence) {}}class Customer{public function __construct(public string $name) {}}class Booking{/** @var list<BookingLine> */private array $lines = [];public function __construct(private Customer $customer) // aggregation: passed in, shared{}public function add(string $service, int $pence): void{$this->lines[] = new BookingLine($service, $pence); // composition: created and owned here}public function total(): int{return array_sum(array_map(fn(BookingLine $l) => $l->pence, $this->lines));}}$booking = new Booking(new Customer("Clare Lefèvre"));$booking->add("Haircut", 2500);$booking->add("Hot towel", 250);echo $booking->total(), "\n"; // 2750
Drawing it: UML: these relationships have standard symbols in UML class diagrams (Unified Modeling Language), which you'll meet in design documents and interviews: a dashed arrow for dependency, a plain line or arrow for association, a line with a hollow diamond for aggregation, and a line with a filled diamond for composition; the diamond sits at the "whole" end. The diagram below shows BookBay's classes from the loosest connection to the tightest, and the table sums up the four.
| Relationship | Means | In code | UML |
|---|---|---|---|
| Dependency | Uses briefly | A method parameter | Dashed arrow |
| Association | Knows | A property holding another, independent object | Line or arrow |
| Aggregation | Has (shared, independent parts) | A property or array of objects passed in | Hollow diamond |
| Composition | Owns (parts live and die with it) | Parts created inside with new | Filled diamond |
BookBay's objects, from the loosest connection to the tightest
ReminderSender ⇢ Notifier
dependency: uses it during send(), doesn't keep it
Booking → Barber
association: knows him; both exist on their own
Booking ◇— Customer
aggregation: has her; she exists before and after
Booking ◆— BookingLine
composition: creates and owns its lines; they die with it
Key Takeaway
Objects connect in four strengths: dependency (uses another briefly, e.g. a parameter), association (keeps a reference to another independent object), aggregation (has parts that also live on their own) and composition (owns parts that are created with it and die with it). Properties holding objects hold references, so a booking should point to its Customer rather than copy her phone number — copy only when you want a frozen snapshot. UML draws them as a dashed arrow, a line, a hollow diamond and a filled diamond.
Why This Matters
How objects are connected decides what happens when something changes. Pointing to the Customer means one change reaches every booking; copying means every copy has to be found. And knowing who owns what tells you who should create an object — and what disappears with it.
These four words — and their UML symbols — are also the vocabulary of design reviews, interviews and every database course you've taken: composition maps to rows that are deleted together, association to foreign keys between independent tables.
A booking now points to its customer, and Clare's reminders go to her new number. Then Mia runs the reminder code on her laptop to test a change — and 20 real customers get a text at 11 p.m. ReminderSender creates new TwilioSms() inside itself, so there's no way to give it a fake sender for tests. That's the next chapter: dependency injection, coupling and cohesion.
