The spread operator (…) lets us merge arrays
and unpack values with clarity and intention.
It removes boilerplate and makes array composition feel natural and expressive.
It reflects a broader shift in modern PHP:
→ structure should be easy to build, easy to read, and free of ceremony.
The spread operator allows us to unpack the elements of one array into another:
$all = [ ...$defaults, ...$overrides, ];
No array_merge.
No function calls.
Just structure expressed directly.
The spread operator reduces:
It makes arrays feel like first‑class building blocks.
The spread operator is structural composition.
It’s not a shortcut.
It’s not syntactic sugar.
It’s a way to express how arrays come together.
The key idea:
→ To use the spread operator when
we want to compose arrays declaratively.
It turns merging into a structural expression rather than a procedural step.
Without the spread operator:
$config = array_merge($defaults, $overrides);
With the spread operator:
$config = [ ...$defaults, ...$overrides, ];
Same meaning.
More clarity.
Less ceremony.
Use it when:
It shines in
controllers, configuration, and any place where arrays represent structure.
Avoid it when:
The spread operator is for composition, not logic.
AI often mixes array_merge and spread syntax inconsistently, or uses the spread operator where keys collide in unexpected ways. Sometimes it spreads arrays that aren’t arrays at all, or nests spreads in ways that obscure meaning.
Our mental model helps us see when spreading expresses real structure
— and when AI is using it simply because it “looks modern”.
The spread operator lets us compose arrays with clarity and intention.
It removes ceremony, improves readability, and makes structure feel natural.
Once we internalize this,
we can immediately see when AI‑generated code:
The spread operator is a small feature
— but it teaches us
how modern PHP wants to express composition with elegance.